2014-07-21 16 views
0

在我onCreate,我的代碼:儘快顯示AdMob插頁,因爲它被裝載

interstitial = new InterstitialAd(this); 
    interstitial.setAdUnitId("ca-app-pub-1852329945819279/4025192744"); 

    // Create ad request. 
    AdRequest adRequest1 = new AdRequest.Builder().build(); 

    // Begin loading your interstitial. 
    interstitial.loadAd(adRequest1); 

哪裏「間隙」是private InterstitialAd interstitial

public void displayInterstitial() { 

    runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      if (interstitial.isLoaded()) { 
       interstitial.show(); 
      } 
      resetInterstitial(); 
     } 
    }); 

} 

如何顯示的間隙(稱之爲displayInterstitial()方法)只要填隙被加載:

我然後使用以下方法展示插頁?所有幫助讚賞。

回答

1

註冊通知您,當間質已加載事件:

interstitial = new InterstitialAd(this); 
interstitial.setAdUnitId("ca-app-pub-1852329945819279/4025192744"); 

interstitial.setAdListener(new AdListener() { 
    @Override 
    public void onAdLoaded() { 
     interstitial.show(); 
    } 

    @Override 
    public void onAdFailedToLoad(int errorCode) { 
     // no interstitial available 
     interstitial = null; 
    } 

}); 

// Create ad request. 
AdRequest adRequest1 = new AdRequest.Builder().build(); 

// Begin loading your interstitial. 
interstitial.loadAd(adRequest1); 

如果我沒有記錯的話,你不需要檢查間隙是否已經被加載(因爲事件僅僅是如果它已被加載,則會引發),並在UI線程中調用它(因此不需要runOnUiThread)。

相關問題