gpt4 book ai didi

android - libGDX 中的奖励视频广告示例

转载 作者:行者123 更新时间:2023-11-29 15:01:31 25 4
gpt4 key购买 nike

我一直在寻找示例,但没有找到。

我找到了一个没有 libgdx 的程序,并解释了为什么它不起作用,但应用程序也没有崩溃。

public class AndroidLauncher extends AndroidApplication implements AdHandler, RewardedVideoAdListener{

...

RewardedVideoAd rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
rewardedVideoAd.setRewardedVideoAdListener(this);

rewardedSetup();

private void rewardedSetup(){
if (!rewardedVideoAd.isLoaded()){
rewardedVideoAd.loadAd("ca-app-pub-683510441556959xxxxxxxxx", new AdRequest.Builder().build());
}

@Override
public void onRewardedVideoAdLoaded() {
if(rewardedVideoAd.isLoaded()){
rewardedVideoAd.show();
}
}

@Override
public void onRewarded(RewardItem rewardItem) {
rewarded(true);
}
}
}

它根本不显示,也许它发生在任何人身上? (我的真实代码充满了处理程序和东西,因此我没有全部放,我猜其他代码对这个问题的影响为 0)

最佳答案

private static final String APP_ID="ca-app-pub-XXXXXX~XXXXX";
private static final String AD_UNIT_ID="ca-app-pub-XXXXX/XXXXXXX";
private RewardedVideoAd mAd;
private boolean isRewardLoaded;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

...

MobileAds.initialize(this, APP_ID);

mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
isRewardLoaded =true;
}

@Override
public void onRewardedVideoAdOpened() {

}

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}

@Override
public void onRewarded(RewardItem rewardItem) {

// call rewards method from here.
loadRewardedVideoAd(); // Load for next Reward Point

}

@Override
public void onRewardedVideoAdLeftApplication() {

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

@Override
public void onRewardedVideoCompleted() {

}
});
loadRewardedVideoAd();
}

private void loadRewardedVideoAd() {
isRewardLoaded=false;
mAd.loadAd(AD_UNIT_ID, new AdRequest.Builder().build());
}

@Override
protected void onResume() {
super.onResume();
mAd.resume(this);
}

@Override
protected void onPause() {
super.onPause();
mAd.pause(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
mAd.destroy(this);
}

设置完成,需要实现AdHandler的两个方法,这两个方法将从core模块调用。

public void showVideoAd(){
runOnUiThread(new Runnable() {
public void run() {

if (mAd.isLoaded()) {
mAd.show();
} else {
loadRewardedVideoAd();
}
}
});
}

public boolean hasVideoReward(){

return isRewardLoaded;
//return mAd.isLoaded(); // -> must be called on the main UI thread.
}

调用 hasVideoReward(),告诉你是否显示奖励面板,showVideoAd() 方法显示奖励视频。

希望您的 AndroidManifest.xml 文件中有这些条目

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />

关于android - libGDX 中的奖励视频广告示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44397952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com