Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wait for 10 second without locking application UI in android [duplicate]

Tags:

java

android

wait

I am stuck with a problem, I want to wait 10 second because I want my application to start the code below after that 10 sec but without stopping that person from clicking anything else in the application (without calling Thread.sleep();).

try {     Log.v("msg", "WAIT CheckFrequencyRun");     Thread.sleep(10000); // giving time to connect to wifi         } catch (InterruptedException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }    //if no network    if(wifiManager.getConnectionInfo().getNetworkId()==-1){     //stop wifi     wifiManager.setWifiEnabled(false);     Log.v("msg", "no connection");     handler.postDelayed(this, checkInterval);    }    //else connection    else{     Log.v("msg", "connection");     onDestroy();    } 
like image 300
DarkVision Avatar asked Jun 21 '13 14:06

DarkVision


People also ask

What is thread sleep in Android Studio?

Thread. sleep() method can be used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can't be negative, else it throws IllegalArgumentException .

Is Android thread safe?

By design, Android View objects are not thread-safe. An app is expected to create, use, and destroy UI objects, all on the main thread. If you try to modify or even reference a UI object in a thread other than the main thread, the result can be exceptions, silent failures, crashes, and other undefined misbehavior.


1 Answers

You can use this:

Handler handler = new Handler(); handler.postDelayed(new Runnable() {     public void run() {      // Actions to do after 10 seconds     } }, 10000); 

For Stop the Handler, You can try this: handler.removeCallbacksAndMessages(null);

like image 83
Recaldev Avatar answered Sep 22 '22 06:09

Recaldev