Programmatically close an interstitial ad

北城以北 提交于 2019-11-27 06:48:33

问题


I want to make sure that interstitial ads on Android, using the Android AdMob SDK, can be closed. After some research, it seems to me that this is not possible due to the ad being a separate activity. What I want to do is close the ad after 5 seconds. Can someone assure me that closing an interstitial ad is impossible or if there's a hack?


回答1:


It's going to be a very late answer but I faced similar issue. A trick can be to call the back button event programmatically.

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

As whenever you press on back button the interstitial ad is closed so firing the back button event will eventually close the interstitial ad. Rest depends on the requirement.




回答2:


It is not possible to programatically close an interstitial ad.




回答3:


For new readers. use:

Intent intent = new Intent(activity, activity.getClass());
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    activity.startActivity(intent);



        @Override
        public void onAdOpened() {
            // Code to be executed when the ad is displayed.
            Log.d("mInterstitialAd", "onAdOpened  ");
            fullscreenAdShowing = true;
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(!fullscreenAdShowing){
                        return;
                    }
                    Log.d("mInterstitialAd", "onAdOpened  Handler 5 seconds run");
                    Intent intent = new Intent(activity, activity.getClass());
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    activity.startActivity(intent);

                }
            }, 5000);
        }

        @Override
        public void onAdClosed() {
            // Code to be executed when when the interstitial ad is closed.
            Log.d("mInterstitialAd", "onAdClosed  loadAd");
            fullscreenAdShowing = false;

            mInterstitialAd.loadAd(new AdRequest.Builder().build());

        }


来源:https://stackoverflow.com/questions/22145885/programmatically-close-an-interstitial-ad

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