I need to assemble Admob in an android program packaged by Cocos creator. When I click the "watch video" button, the rewarded video plays fine. But if I "close video" then try to "watch video" again, it does not work. When I debug the program, I find this code can not run again.
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.");
}
My loadAwardAd code is:
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.");
}
});
}
I reload the loadAwardAd() in onAdDismissedFullScreenContent():
@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();
}
I guess the reason is that I use new Myapplication().loadAwardAd(), but I must use the static method in showRewardedVideo()
My showRewardedVideo() is:
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();
}
});
}
});
}
This code refer to the official example code: GitHub. The only different place is I use the static method to show the awarded ad. But the cocos' creator only can use the static method. I don't know how to solve it. I