2017-03-04 51 views
1

我正在编辑现有android应用程序(不是游戏)的代码。我想在显示一定数量的屏幕或操作后显示插页式广告(请遵守Google指南)。如何在x操作后加载插页式广告

这是怎么了,我通常加载我的插页广告:

// 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); 
    } 

回答

2

跟踪的使用int行动,使该将它加一个方法,并检查您是否应该表现出一个广告。如果是,请展示广告并将其重置。

private static int x = 0; 

public static void incrementActions() { 
    x++; 
    if(x >= 5) { 
     x = 0; 
     displayInterstital(); 
    } 
} 

把它放在你的MainActivity中,例如这样调用它:MainActivity.incrementActions()。 也让你的displayInterstital()方法静态,或给该方法一个对象的类它是在。

+0

谢谢,但变量x它不会改变他的价值,如果我把这个功能就这样! –

+0

x ++是一样的x = x + 1 –

+0

是的,它增加它,我在哪里必须把这个代码? –