首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Interstitial广告

Android Interstitial广告
EN

Stack Overflow用户
提问于 2015-06-13 20:40:31
回答 1查看 117关注 0票数 1

我对android的Interstitial广告有个问题。当应用程序启动时,广告显示,然后当用户移动到另一个活动并返回到主活动时,广告再次显示。如果广告在主活动中只显示一次,直到下一次重新启动,即使用户移动到另一个活动,我应该做什么呢?谢谢!这是我的密码:

代码语言:javascript
复制
  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();
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-13 21:30:04

为应用程序创建一个应用程序类并执行以下操作

代码语言:javascript
复制
public class MyApplication extends Application{

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

        }    
}

然后在你的主要活动中

代码语言:javascript
复制
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();
     }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30823327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档