Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically close an interstitial ad

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?

like image 296
Driss Bounouar Avatar asked Mar 03 '14 11:03

Driss Bounouar


1 Answers

You can get a reference of the Admob Interstitial activity and call finish() on it when you want to close it, by implementing ActivityLifecycleCallbacks:

private final Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            Log.e(TAG, "onActivityCreated: " + activity.getClass());
        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {
            Log.e(TAG, "onActivityStarted: " + activity.getClass());
        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            Log.e(TAG, "onActivityResumed: " + activity.getClass());
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            Log.e(TAG, "onActivityPaused: " + activity.getClass());
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {
            Log.e(TAG, "onActivityStopped: " + activity.getClass());
        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
            Log.e(TAG, "onActivitySaveInstanceState: " + activity.getClass());
        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
            Log.e(TAG, "onActivityDestroyed: " + activity.getClass());
        }
    };


MyApplication.getInstance().registerActivityLifecycleCallbacks(activityLifecycleCallbacks);


E/TAG: onActivityCreated: class com.google.android.gms.ads.AdActivity
E/TAG: onActivityStarted: class com.google.android.gms.ads.AdActivity
E/TAG: onActivityResumed: class com.google.android.gms.ads.AdActivity
like image 178
Goran Horia Mihail Avatar answered Oct 04 '22 16:10

Goran Horia Mihail