'How to auto-close app open ads after splash

I placed an ad of the type: "app-open" admob. This advertisement is not accepted by the App Store except in one case: That there is a splash for the application, and that the advertisement appears above the splash and ends with the end of the splash.

For example: Here I set the time to run the ad after the splash appears

  /** LifecycleObserver methods */
    @OnLifecycleEvent(ON_START)
    public void onStart() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showAdIfAvailable();
            }
        }, 1000);

        Log.d(LOG_TAG, "onStart");
    }

Here is the total splash timing:

 Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (go) {
                startActivity(new Intent(Splash_Activity.this, Home_main.class));
                finish(); 
                //If you delete the finish also the splash appears after exiting the application, how can this process be transferred to the advertisement
            }
        }
    }, 4000);
}

So if you are asking where is the problem: The problem is: If you do not close the ad, it will close automatically at the end of the time of the splash, but when you exit the application you find the ad and you need to close it.

What I need: It is an automatic closure and a finish for the advertisement when the splash timer ends.

AppOpenManager.clss

  /** Prefetches App Open Ads. */
    public class AppOpenManager implements LifecycleObserver, Application.ActivityLifecycleCallbacks {
    
    
    
        private static final String LOG_TAG = "AppOpenManager";
        private static final String AD_UNIT_ID = "MY APP AD UNIT ID";
        private AppOpenAd appOpenAd = null;
        private long loadTime = 0;
    
        private AppOpenAd.AppOpenAdLoadCallback loadCallback;
        private Activity currentActivity;
        private static boolean isShowingAd = false;
    
        private final GlobalVar home_main;
    
        /**
         * Constructor
         */
        public AppOpenManager(GlobalVar home_main) {
            this.home_main = home_main;
            this.home_main.registerActivityLifecycleCallbacks(this);
            ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        }
    
        public static void showAdIfAvailable(FullScreenContentCallback fullScreenContentCallback) {
        }
    
        /** LifecycleObserver methods */
        @OnLifecycleEvent(ON_START)
        public void onStart() {
    
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showAdIfAvailable();
    
                }
            }, 1000);
    
    
            Log.d(LOG_TAG, "onStart");
        }
    
        /** Shows the ad if one isn't already showing. */
        public void showAdIfAvailable() {
            // Only show ad if there is not already an app open ad currently showing
            // and an ad is available.
            if (!isShowingAd && isAdAvailable()) {
                Log.d(LOG_TAG, "Will show ad.");
    
                FullScreenContentCallback fullScreenContentCallback =
                        new FullScreenContentCallback() {
                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Set the reference to null so isAdAvailable() returns false.
                                AppOpenManager.this.appOpenAd = null;
                                isShowingAd = false;
                                fetchAd();
                            }
    
                            @Override
                            public void onAdFailedToShowFullScreenContent(AdError adError) {}
    
                            @Override
                            public void onAdShowedFullScreenContent() {
                                isShowingAd = true;
                            }
                        };
    
                appOpenAd.setFullScreenContentCallback(fullScreenContentCallback);
                appOpenAd.show(currentActivity);
    
            } else {
                Log.d(LOG_TAG, "Can not show ad.");
                fetchAd();
            }
        }
    
    
        /**
         * Request an ad
         */
        public void fetchAd() {
    
    
            // Have unused ad, no need to fetch another.
            if (isAdAvailable()) {
                return;
            }
    
            loadCallback =
                    new AppOpenAd.AppOpenAdLoadCallback() {
                        /**
                         * Called when an app open ad has loaded.
                         *
                         * @param ad the loaded app open ad.
                         */
                        @Override
                        public void onAdLoaded(AppOpenAd ad) {
                            AppOpenManager.this.appOpenAd = ad;
                            AppOpenManager.this.loadTime = (new Date()).getTime();
                        }
    
                        /**
                         * Called when an app open ad has failed to load.
                         *
                         * @param loadAdError the error.
                         */
                        @Override
                        public void onAdFailedToLoad(LoadAdError loadAdError) {
                            // Handle the error.
                        }
    
                    };
            AdRequest request = getAdRequest();
            AppOpenAd.load(
                    home_main, AD_UNIT_ID, request,
                    AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
        }
        // We will implement this below.
    
    
        /**
         * Creates and returns ad request.
         */
        private AdRequest getAdRequest() {
            return new AdRequest.Builder().build();
        }
    
        /**
         * Utility method that checks if ad exists and can be shown.
         */
        public boolean isAdAvailable() {
            return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }
    
        @Override
        public void onActivityStarted(Activity activity) {
            currentActivity = activity;
        }
    
        @Override
        public void onActivityResumed(Activity activity) {
            currentActivity = activity;
        }
    
        @Override
        public void onActivityStopped(Activity activity) {
        }
    
        @Override
        public void onActivityPaused(Activity activity) {
        }
    
        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        }
    
        @Override
        public void onActivityDestroyed(Activity activity) {
            currentActivity = null;
        }
        /** Utility method to check if ad was loaded more than n hours ago. */
        private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
            long dateDifference = (new Date()).getTime() - this.loadTime;
            long numMilliSecondsPerHour = 3600000;
            return (dateDifference < (numMilliSecondsPerHour * numHours));
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source