1.主要流程不说了,官方很详细(如果要在谷歌市场上架,请一定按照规定集成,否则有下架风险)
主要代码分享一下
private String TAG = "AdmobLog------>";
private RewardedAd rewardedAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
rewardedAd = new RewardedAd(this,
"ca-app-pub-3940256099942544/5224354917");
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
// Ad successfully loaded.
Log.d(TAG,"加载成功");
}
@Override
public void onRewardedAdFailedToLoad(int errorCode) {
// Ad failed to load.
Log.d(TAG,"加载失败");
}
};
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
}
public void button(View view) {
if (rewardedAd.isLoaded()) {
Activity activityContext = MainActivity.this;
RewardedAdCallback adCallback = new RewardedAdCallback() {
@Override
public void onRewardedAdOpened() {
// Ad opened.
Log.d(TAG,"广告打开");
}
@Override
public void onRewardedAdClosed() {
// Ad closed.
Log.d(TAG,"广告关闭");
}
@Override
public void onUserEarnedReward(@NonNull RewardItem reward) {
// User earned reward.
Log.d(TAG,"获得奖励");
}
@Override
public void onRewardedAdFailedToShow(int errorCode) {
// Ad failed to display.
Log.d(TAG,"失败播放");
}
};
rewardedAd.show(activityContext, adCallback);
onRewardedAdClosed();
} else {
Log.d(TAG, "奖励广告尚未加载");
}
}
public RewardedAd createAndLoadRewardedAd() {
RewardedAd rewardedAd = new RewardedAd(this,
"ca-app-pub-3940256099942544/5224354917");
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
// Ad successfully loaded.
Log.d(TAG,"预加载成功");
}
@Override
public void onRewardedAdFailedToLoad(int errorCode) {
// Ad failed to load.
Log.d(TAG,"预加载失败");
}
};
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
return rewardedAd;
}
public void onRewardedAdClosed() {
this.rewardedAd = createAndLoadRewardedAd();
}
网友评论