[英]How to load Interstitial ad after x actions
我正在編輯現有android應用程序(而不是游戲)的代碼。 我想在執行一定數量的屏幕或操作后顯示插頁式廣告(請參閱Google指南)。
通常,這是我加載插頁式廣告的方式:
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(News_Detail.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
AdRequest adRequest = new AdRequest.Builder().build();
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Your code to show add
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}, 20000);
}
使用int
跟蹤動作,制作增加它的方法,並檢查是否應該展示廣告。 如果是,則顯示廣告並重置。
private static int x = 0;
public static void incrementActions() {
x++;
if(x >= 5) {
x = 0;
displayInterstital();
}
}
例如,將其放在您的MainActivity中,並按如下方式調用它: MainActivity.incrementActions()
。 還可以使displayInterstital()
方法為靜態方法,或為該方法提供其所屬類的Object。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.