\$\begingroup\$
\$\endgroup\$
I want to load Interstitial AdMob after 5 second, after the Activity started, in another Thread
. Is this code right, or I'm duplicating Runnable
s? 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
1 Answer 1
\$\begingroup\$
\$\endgroup\$
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
lang-java