具有setContentView的插页式广告

时间:2015-09-27 17:53:15

标签: android admob interstitial setcontentview

我正在尝试为使用setContentView的应用添加插页式广告。我已经实现了一个“监听器”,可以在需要广告时正确调用。您可以通过调用此函数“Listener”{...}来缩写您的答案,并假设每当应用为广告发出信号时,括号内的所有内容都会出现。以下答案对横幅广告有一个很好的方法。

Implementing Admob banner when setContentView() is used for the Surfaceview

我的问题是,在这种情况下,我应该如何实现插页式广告?

我的代码基本上是链接的答案,加上:

...    
        setContentView(layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        listener= new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent event) {
                if (event.getPropertyName()=="sharing1") {
                    caring();
                    game.dead=false;
                    game.sharing1=false;
                }
                if(event.getPropertyName()=="dead")
                {
                //Make an interstitial ad here
                }
            }
        };

1 个答案:

答案 0 :(得分:0)

不确定您要求的是什么,但与横幅广告不同,插页式广告是一种弹出窗口,独立于基础活动的内容视图显示。

调用#setContentView()后可以调用函数。也许您应该发布一些代码,以便我们知道您面临的具体问题

<强>更新 您可以仅在需要时预加载并显示它,在这种情况下,当玩家“死亡”时。这将减少延迟。每次展示广告后,您都必须重新加载广告,以便在后续调用时显示。此外,如果您的侦听器将在非UI线程中调用,请使用处理程序。这是一个解决方案:

...   



  InterstitialAd interstitial; 
        setContentView(layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        InterstitialAd interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("your-ad-unit");
        AdRequest adRequest = new AdRequest.Builder().build();
        // Preload your interstitial to avoid load time delays 
        interstitial.loadAd(adRequest);
        listener= new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent event) {
                if (event.getPropertyName()=="sharing1") {
                    caring();
                    game.dead=false;
                    game.sharing1=false;
                }
                if(event.getPropertyName()=="dead")
                {
                //Make an interstitial ad here
        runOnUiThread(new Runnable() {
        @Override
        public void run() {
        if (interstitial != null && interstitial.isLoaded()){
             interstitial.show();
             //reload ad 
             interstitial.loadAd(new AdRequest.Builder().build());             
             }  
        }
    });
                }
            }
        };
相关问题