我需要在cocos creator打包的android程序中组装admob
当我点击“观看视频”按钮时,奖励视频播放正常。
但是如果我“关闭视频”然后尝试再次“观看视频”它不起作用。
当我调试程序时,我发现这段代码无法再次运行。
new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
MyApplication.this.rewardedAd = ad;
Log.d(TAG, "Ad was loaded.");
}
我的loadAwardAd代码是
public void loadAwardAd(){
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest,
new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
MyApplication.this.rewardedAd = ad;
Log.d(TAG, "Ad was loaded.");
}
});
}
我在 onAdDismissedFullScreenContent() 中重新加载 loadAwardAd():
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedAd = null;
MyApplication.getInstance().loadAwardAd();
}
我想原因是我使用了
new Myapplication().loadAwardAd()
,但我必须在showRewardedVideo()中使用静态方法。
我的 showRewardedVideo() 是
public static void showRewardedVideo(){
if (rewardedAd == null) {
Log.d("TAG", "The rewarded ad wasn't ready yet.");
return;
}
rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedAd = null;
MyApplication.getInstance().loadAwardAd();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
Activity activityContext = (AppActivity)MyApplication.getInstance().mainActive;
// Make sure to operate on the UI thread
activityContext.runOnUiThread(new Runnable() {
@Override
public void run() {
rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
}
});
}
此代码参考官方示例代码(https://github.com/googleads/googleads-mobile-android-examples/blob/main/java/admob/RewardedVideoExample/app/src/main/java/com/google /ads/rewardedvideoexample/MainActivity.java)。唯一不同的地方是我使用静态方法显示获奖广告。但是cocos creator只能使用静态方法。我不知道如何解决它。我