加载后立即显示AdMob插页式广告

时间:2014-07-21 20:59:57

标签: java android interstitial

在我的onCreate中,我有代码:

    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 个答案:

答案 0 :(得分:2)

注册在加载插页式广告时通知您的事件:

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 )。