How to make Admob interstitial show without delay?

南楼画角 提交于 2020-01-06 12:45:06

问题


I have a simple game made in android sdk. When the user has level failed I want an interstitial to appear, more or less exactly at that moment. The problem is from the code I have, is a @5 seconds delay from the moment that interstitial function start until interstitial appear(checked in LogCat. Here is the code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
        // Create the interstitial
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));

        // Create ad request.
        adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
    }
    //initialise banner ad
    this.BANNER_AD_UNIT_ID = getResources().getString(R.string.BannerAd_unit_id);
    showBanner();

}

public void openAd() {
    if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
        runOnUiThread(new Runnable() {
            public void run() {
                if (!interstitial.isLoaded()) {
                    interstitial.loadAd(adRequest);
                }
                interstitial.setAdListener(new AdListener() {
                    public void onAdLoaded() {
                        interstitial.show();
                    }

                });

            }
        });
    }
}

Is there any way to cache the interstitial before that function is called. That delay is really bad.... Thanks!


回答1:


Your problem is that you are attempting to load the ad and show it at the same time. Loading requires a network request which is slow. This is never going to give you the experience you are after.

Recommended practice is to call interstitial.loadAd(adRequest) at the start of your game or level. And then at the end of your level call

if (interstitial.isAdLoaded() {
    interstitial.show();
}


来源:https://stackoverflow.com/questions/27891000/how-to-make-admob-interstitial-show-without-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!