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();
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 27, 2017 at 3:53
\$\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.

answered Jan 31, 2017 at 12:21
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.