2
\$\begingroup\$

I want to load Interstitial AdMob after 5 second, after the Activity started, in another Thread. Is this code right, or I'm duplicating Runnables? Is there a better way?

    Handler handler;

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


    // loading ads
    handler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadInterstitialAd();
                }
            }, 5000);

        }
    };

    Thread thread = new Thread(runnable);
    thread.setName("AdThread");
    thread.start();

}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

There is no need to use Thread class you can directly do this:

// loading ads
    handler = new Handler();

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadInterstitialAd();
                }
            }, 5000);

So what is happening your handler will schedule your Runnable after 5 sec, you don't have to instanciate Thread and call runnable again.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.