Search code examples
androidandroid-studioadsinterstitial

How to Show Interstitial Ad every X Seconds


I need to show interstitial ad in my app in every x seconds. I had gated this code. Its working fine, but the problem is, interstitial ads still show up even if the app closed.

How can I can stop this when the app is closed?

Thank you.

public class MainActivity extends AppCompatActivity {

    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        prepareAd();

        ScheduledExecutorService scheduler =
                Executors.newSingleThreadScheduledExecutor();

        scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() {
                Log.i("hello", "world");
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (mInterstitialAd.isLoaded()) {
                            mInterstitialAd.show();
                        } else {
                           Log.d("TAG"," Interstitial not loaded");
                        }

                        prepareAd();
                    }
                });
            }
        }, 10, 10, TimeUnit.SECONDS);
    }

    public void  prepareAd() {
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    }
}

Solution

  • Seems like your activity is in background and then user will be able to see the ads because once your activity is destroyed then your ads won't be able to display , no this context no activity.

    First : keep a reference to ScheduledExecutorService outside onCreate

    Second : Override onStop and invoke scheduler.shutdownNow().

    onStop : it will be invoked when your activity will go into the background state

    shutdownNow() : will attempts to stop currently running tasks and stop the execution of waiting tasks

    so this will stop the executor even if your app is in background

    public class MainActivity extends AppCompatActivity {
    
        private InterstitialAd mInterstitialAd;
        private ScheduledExecutorService scheduler;
        private boolean isVisible;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            prepareAd();
        }
    
    
        @Override
        protected void onStart(){
        super.onStart();
            isVisible = true;
            if(scheduler == null){
                scheduler = Executors.newSingleThreadScheduledExecutor();
                scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() {
                Log.i("hello", "world");
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (mInterstitialAd.isLoaded() && isVisible) {
                            mInterstitialAd.show();
                        } else {
                           Log.d("TAG"," Interstitial not loaded");
                        }
    
                        prepareAd();
                    }
                });
            }
        }, 10, 10, TimeUnit.SECONDS);
    
            }
    
        }    
        //.. code 
    
        @Override
        protected void onStop() {
            super.onStop();
            scheduler.shutdownNow();
            scheduler = null;
            isVisible =false;
       }
    
    }