Android的插页式广告 [英] Android Interstitial ad

查看:254
本文介绍了Android的插页式广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有android的插页式广告的问题。当应用程序启动,广告节目,然后当用户移动到另一个活动,并返回到主要活动,广告再次出现。我应该怎么做的广告只显示一次在主要活动中,从来没有再次表明,直到下次重新启动,即使用户移动到另一个活动?谢谢!这是我的code:

 公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.tabpmnth_xm);    //创建的间隙。
    间质性=新在InterstitialAd(本);
    interstitial.setAdUnitId(CA-APP-酒馆xxxxxxxxxxxx8x / x2xxxxxxxx);    AdRequest中aadRequest =新AdRequest.Builder()
    。建立();    //开始加载插页式。
    interstitial.loadAd(aadRequest);    interstitial.setAdListener(新AdListener的(){
          公共无效onAdLoaded(){
               displayInterstitial();
          }
   });    公共无效displayInterstitial(){
     如果(interstitial.isLoaded()){
      interstitial.show();
    }
  }


解决方案

为您的应用程序创建一个应用程序类,并做到这一点。

 公共类MyApplication的扩展应用{    @覆盖
        公共无效的onCreate(){
        super.onCreate();
        。preferenceManager.getDefaultShared preferences(本).edit()putBoolean(ShowInterstitial,TRUE)。适用();        }
}

然后在您的主要活动

 公共无效displayInterstitial(){
     共享preferences SP = preferenceManager.getDefaultShared preferences(本);
     如果(interstitial.isLoaded()及&放大器; sp.getBoolean(ShowInterstitial,真)){
      interstitial.show();
      。preferenceManager.getDefaultShared preferences(本).edit()putBoolean(ShowInterstitial,假)。适用();
     }
}

i have a problem with Interstitial ad in android. When the application launches, the ad shows and then when the user moves to another activity and returns back to Main Activity, the ad shows again. What should i do that the ad only shows once in Main Activity and never shows again till next restart even if the user move to another activity? Thank you! This is my code:

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

解决方案

Create an Application class for your app and do this

public class MyApplication extends Application{

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

        }    
}

then in your main activity

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

这篇关于Android的插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆