我试图在游戏结束时投放插页式广告。但是 插页式广告总是显示在游戏的中间。我认为这会给用户带来烦恼
这是在游戏结束时显示admob插页式广告的代码
初始化插页式广告的方法
这是main.java文件
// AdMob
AdView adMob_smart;
InterstitialAd adMob_interstitial;
final boolean show_admob_smart = true; // show AdMob Smart banner
final boolean show_admob_interstitial = true; // show AdMob Interstitial
// add_admob_smart
void add_admob_smart() {
if (show_admob_smart
&& ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null) {
adMob_smart = new AdView(this);
adMob_smart.setAdUnitId(getString(R.string.adMob_smart));
adMob_smart.setAdSize(AdSize.SMART_BANNER);
((ViewGroup) findViewById(R.id.admob)).addView(adMob_smart);
com.google.android.gms.ads.AdRequest.Builder builder = new AdRequest.Builder();
// builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("4d0555dfcad9b000");
adMob_smart.loadAd(builder.build());
}
}
// add_admob_interstitial
void add_admob_interstitial() {
if (show_admob_interstitial
&& ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null) {
adMob_interstitial = new InterstitialAd(this);
adMob_interstitial.setAdUnitId(getString(R.string.adMob_interstitial));
com.google.android.gms.ads.AdRequest.Builder builder = new AdRequest.Builder();
// builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("4d0555dfcad9b000");
adMob_interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
adMob_interstitial.show();
}
});
adMob_interstitial.loadAd(builder.build());
}
}
答案 0 :(得分:0)
您应该使用一个标志,例如gameOver。
if (gameOver){
adMob_interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
adMob_interstitial.show();
}
});
}
答案 1 :(得分:0)
if (gameOver){
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}