2015-09-27 84 views
0

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

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

回答

0

不知道你要问什么,但不像横幅广告,插页广告是一个弹出其独立的显示基本活动的内容视图。

你可以在调用#setContentView()之后调用你的函数。也许你应该张贴一些代码,所以我们知道哪些具体的问题你面对

UPDATE: 您可以预加载和显示它仅在需要的时候,在这种情况下,当玩家“死亡”。这会减少延迟。每次展示广告后,您必须重新加载广告,以便在随后的调用中显示广告。另外,如果您的侦听器将在非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());    
      } 
     } 
    }); 
       } 
      } 
     }; 
+0

代码本质上就是链接的。唯一的区别是我有一个可以多次致电广告的“听众”。 –

+0

@ArriFerrari好的。我修改了我的答案。希望它有帮助 –

+0

嗯,我遇到的问题是通过使用setContentView();我似乎在阻止广告。 –