InterstitialAd应在每次曝光后加载

时间:2014-08-15 10:16:57

标签: android admob

我需要在我的应用中使用InterstitialAd。但问题是,在第一次曝光之后,在显示我正在制作广告之前,请使用请求调用loadAd方法,然后在适当的时刻调用show(调用show方法)。一切似乎都运行良好,但是当我尝试下次调用show方法时,由于isLoaded返回false,它不起作用。 有没有办法只加载一次广告。

编辑

谢谢答案,但主要问题是loadAd是网络连接,但谷歌建议不要在主线程中执行互联网任务,那么它是在主ui线程中从互联网加载广告的。它导致我的应用程序滞后。他们自相矛盾。

1 个答案:

答案 0 :(得分:0)

您必须在广告关闭时重新加载广告。我定义了一个方法" reloadInterstitial()"并在onCreate()方法中调用它。它很棒。您只需要在显示广告时调用displayInterstitial():

public class MyActivityClass extends Activity{
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       interstitial = new InterstitialAd(MyActivityClass.this);
        interstitial.setAdUnitId("*****");
         reloadInterstitial();
        // Create ad request
        AdRequest adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial
        interstitial.loadAd(adRequest);
  }

public void displayInterstitial(){  
        if(interstitial != null && interstitial.isLoaded())
            interstitial.show();
    }

private void reloadInterstitial(){
        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // TODO Auto-generated method stub
                super.onAdLoaded();
            }
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // TODO Auto-generated method stub
                super.onAdFailedToLoad(errorCode);
                 interstitial = new InterstitialAd(MyActivityClass.this);
                    interstitial.setAdUnitId("*****");
                    // Begin loading your interstitial
                    interstitial.loadAd(new AdRequest.Builder().build());
                    loadInterCallBacks();
            }

            @Override
            public void onAdOpened() {
                // TODO Auto-generated method stub
                super.onAdOpened();
            }

            @Override
            public void onAdClosed() {
                // TODO Auto-generated method stub
                super.onAdClosed();
                interstitial = new InterstitialAd(MyActivityClass.this);
                interstitial.setAdUnitId("****");
                loadInterCallBacks();
                // Begin loading your interstitial
                interstitial.loadAd(new AdRequest.Builder().build());
            }

            @Override
            public void onAdLeftApplication() {
                // TODO Auto-generated method stub
                super.onAdLeftApplication();
            }
        });

    }


 private void loadInterCallBacks(){
        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // TODO Auto-generated method stub
                super.onAdLoaded();
            }
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // TODO Auto-generated method stub
                super.onAdFailedToLoad(errorCode);
                 interstitial = new InterstitialAd(MyActivityClass.this);
                    interstitial.setAdUnitId("xxxxxxxxxxx");
                    // Begin loading your interstitial
                    interstitial.loadAd(new AdRequest.Builder().build());
                    loadInterCallBacks();
            }

            @Override
            public void onAdOpened() {
                // TODO Auto-generated method stub
                super.onAdOpened();
            }

            @Override
            public void onAdClosed() {
                // TODO Auto-generated method stub
                super.onAdClosed();
                interstitial = new InterstitialAd(MyActivityClass.this);
                interstitial.setAdUnitId("xxxxxx");
                loadInterCallBacks();
                // Begin loading your interstitial
                interstitial.loadAd(new AdRequest.Builder().build());
            }

            @Override
            public void onAdLeftApplication() {
                // TODO Auto-generated method stub
                super.onAdLeftApplication();
            }
        });

    }