Android 安卓间质广告

Android 安卓间质广告,android,Android,我对android中的插入式广告有问题。当应用程序启动时,广告会显示,然后当用户移动到另一个活动并返回到主活动时,广告会再次显示。我应该怎么做才能使广告在主活动中只显示一次,并且在下次重新启动之前不会再显示,即使用户移动到另一个活动?非常感谢。这是我的代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout

我对android中的插入式广告有问题。当应用程序启动时,广告会显示,然后当用户移动到另一个活动并返回到主活动时,广告会再次显示。我应该怎么做才能使广告在主活动中只显示一次,并且在下次重新启动之前不会再显示,即使用户移动到另一个活动?非常感谢。这是我的代码:

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabpmnth_xm);

    // Create the interstitial.
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("ca-app-pub-xxxxxxxxxxxx8x/x2xxxxxxxx");

    AdRequest aadRequest = new AdRequest.Builder()
    .build();

    // Begin loading your interstitial.
    interstitial.loadAd(aadRequest);

    interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
   });

    public void displayInterstitial() {
     if (interstitial.isLoaded()) {
      interstitial.show();
    }
  }

为应用程序创建应用程序类并执行此操作

public class MyApplication extends Application{

    @Override
        public void onCreate(){
        super.onCreate();
        PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("ShowInterstitial", TRUE).apply();

        }    
}
然后在你的主要活动中

public void displayInterstitial() {
     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
     if (interstitial.isLoaded() && sp.getBoolean("ShowInterstitial", true)){
      interstitial.show();
      PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("ShowInterstitial", false).apply();
     }
}