2014-04-28 68 views
2

這裏有幾個問題和幾個不同的答案 - 這些問題都不適用於我。直到第二次請求時,Admob纔會顯示廣告

我有一個libdgx應用程序,這讓我猜稍微複雜一些,但我沒有看到它應該導致問題。我的onCreate代碼如下。

我有一個AdListener分配,當add被加載時被調用。此代碼正在被打中,但沒有顯示任何內容。如果我將廣告設置爲刷新,則會在第一次刷新後顯示。如果我在短暫等待後再次調用loadAd,則會顯示橫幅。如果我連續兩行代碼調用loadAd,則廣告不會顯示。任何指針可能會發生在這裏將不勝感激。

編輯:我也只是注意到,我可以點擊廣告,即使我看不到廣告。當我點擊廣告並返回遊戲時,廣告正確顯示。

edit2:我將廣告更改爲SMART_BANNER而不是BANNER,這似乎按預期工作。我仍然不知道爲什麼BANNER不工作。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Create the layout 
    FrameLayout layout = new FrameLayout(this); 


    // Do the stuff that initialize() would do for you 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN 
        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN 
        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
    ); 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
    config.useAccelerometer = false; 
    config.useCompass = false; 
    config.useWakelock = false; 
    config.useImmersiveMode = true; 
    config.hideStatusBar = true; 

    // Create the libgdx View 
    View gameView = initializeForView(new WordStormGame(this), config); 

    // Add the libgdx view 
    layout.addView(gameView); 

    adView = new AdView(this); 
    adView.setAdUnitId("xxxxxxxxxxxx"); 
    adView.setAdSize(AdSize.BANNER); 
    adView.setAdListener(new AdListener() { 
          public void onAdLoaded() 
           Gdx.app.log("Advert", "Ad Loaded"); 
          } 
         } 
    ); 

    // Add the AdMob view 
    FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
      FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.TOP | android.view.Gravity.CENTER_HORIZONTAL); 
    layout.addView(adView, adsParams); 

    // Hook it all up 
    setContentView(layout); 

    adView.loadAd(new AdRequest.Builder().build()); 
} 

回答

5

解決,這是最終爲增加線路

adView.setBackgroundColor(Color.TRANSPARENT); 

我最後的AD瀏覽創建代碼看起來像下面這樣簡單:

private void createAdView() { 
    abBuilder = new AdRequest.Builder(); 
    abBuilder.addTestDevice("xxxxxxx"); 

    if (adView != null) 
     ((ViewManager) adView.getParent()).removeView(adView); 

    adView = new AdView(this); 
    adView.setAdUnitId("xxxxxxx"); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    adView.setAdListener(new AdListener() { 
          public void onAdLoaded() { 
           if (adsShown) 
            adView.setVisibility(View.VISIBLE); 
           Gdx.app.log("Advert", "Ad Loaded"); 
          } 
         } 
    ); 

    adView.setBackgroundColor(Color.TRANSPARENT); 

    // Add the AdMob view 
    RelativeLayout.LayoutParams adParams = 
      new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 

    layout.addView(adView, adParams); 
} 
+0

謝謝@威爾。你拯救了我的一天。你的技巧幫了大忙 – MicroEyes

0

只是一個想法,你有沒有包括這個代碼?它是所有廣告都需要的。

public void onResume() { 
     super.onResume(); 
     if (adView != null) { 
      adView.resume(); 
     } 
    } 

    @Override 
    public void onPause() { 
     if (adView != null) { 
      adView.pause(); 
     } 
     super.onPause(); 
    } 

    /** Called before the activity is destroyed. */ 
    @Override 
    public void onDestroy() { 
     // Destroy the AdView. 
     if (adView != null) { 
      adView.destroy(); 
     } 
     super.onDestroy(); 
    } 
+0

沒有區別很遺憾。 –

0

我有聰明的一面旗幟完全相同的問題,我通過添加視圖佈局直通的AdListener的一次廣告加載固定它:

 // Create and setup the AdMob view 
     AdRequest.Builder ad_builder = new AdRequest.Builder(); 
     ad_builder.addTestDevice("XXXXXXXXXXXXX"); // my HTC phone 
     m_AdRequest = ad_builder.build(); 

     m_AdView = new AdView(this); 
     m_AdView.setAdSize(AdSize.SMART_BANNER); 
     m_AdView.setAdUnitId("ZZZZZZZZZZZZ"); 
     m_AdView.setAdListener(new AdListener() { 
      private boolean _first = true; 

      @Override 
      public void onAdLoaded() { 
       if (_first) { 
        _first = false; 

        // Add the AdMob view when the first ad gets loaded, this makes sure the first ad gets displayed 
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // places the ad at the bottom overlapping your game 

        layout.addView(m_AdView, adParams); 
       } 
      } 
     }); 

希望這能解決你的頭痛!

+1

謝謝你。我最終找到了一個解決方案,並忘記在這裏添加它。我剛剛這樣做。乾杯。 –