我在我的应用程序中使用AdMob Integrated制作Android应用程序,我使用AdMob在加载WebView 20秒后显示广告。问题是,当用户在20秒内关闭应用程序时,插页式广告仍会显示,这与Google AdMob政策相反,如何在用户退出活动或应用时进行此操作,不应显示插页式广告
这是我的代码:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
interAd = new InterstitialAd(MainActivity.this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
interAd.show();
}
});
interAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
}
});
}
} , 20000);
答案 0 :(得分:0)
添加onBackpress和onDestroy overides并删除处理程序回调并在每个回调中将interstitialad变量设置为null。
@Override
protected void onBackpressed() {
handler.removeCallbacks(your handler name here);
interAd =null
super.onBackpressed();
}
@Override
protected void onDestroy() {
handler.removeCallbacks(your handler name here);
interAd =null
super.onDestroy();
}
如果你想在你的应用程序进入后台时也要处理这个问题,那么也要添加onPause。
@Override
protected void onPause() {
handler.removeCallbacks(your handler name here);
interAd =null
super.onPause();
}
希望有所帮助。
如果您需要进一步的帮助,请与我们联系。
答案 1 :(得分:0)
在onDestroy调用
时传递你的处理程序Handler myHandler = new Handler().postDelayed(new Runnable() {
@Override
public void run() {
interAd = new InterstitialAd(MainActivity.this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
if(interAd.isLoaded() && interAd !=null)
interAd.show();
}
});
interAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
}
});
}
} , 20000);
@Override
protected void onDestroy() {
Log.d("MainActivty","onDestroy removing callbacks...");
handler.removeCallbacks(myHandler);
interAd =null
super.onDestroy();
}