Android – how to display insert ads every x seconds

I need to display insert ads in my app every x seconds. I've closed this code. It works normally, but the problem is that insert ads will still be displayed even if the app is closed

How can I stop this operation after closing the application?

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());
    }
}

resolvent:

Your activity seems to be in the background, and then users can see the advertisement, because once your activity is destroyed, your advertisement cannot be displayed. There is no activity without this context

First: keep the reference to scheduledexecutorservice outside oncreate

Second: override onstop and call scheduler. Shutdownnow()

Onstop: called when your activity enters the background state

Shutdown now(): will attempt to stop the currently running task and stop executing the waiting task

Therefore, even if your application is in the background, it will stop executing the program

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;
   }

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>