Reputation: 507
Learning Android development.
Code has to wait for few seconds/minutes/hours before going to the next statement in a for
Loop.
for( i=0; i<number; i++) {
// Do Something
// Then Wait for x hours, y minutes, and z seconds. Then proceed to next command.
// Do some more things.
} //End for loop.
I searched for this but found many answers like thread.Sleep
, Sleep
, try{wait(); } Catch{ }
, etc...
Also, found out about Handler
. Can I use a Handler
inside a for
loop??
Rather, is there a simple command like wait(x hours, x minutes, x seconds); something like this??
Please help!!
Upvotes: 0
Views: 2010
Reputation: 71
In Android there is a class that can do all what you are saying, AsyncTask.
private class YourTaskClassName extends AsyncTask<Void, Integer, Long> {
protected Long doInBackground(Void.. values) {
//Here is where you do the loop
for (int i = 0; i < number; i++) {
...
publishProgress(yourProgress); //Value passed to onProgressUpdate
}
return totalSize; //Value for onPostExecute
}
protected void onProgressUpdate(Integer... progress) {
//Here is what you wanna show while your loop is running in background
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
//Here is what you wanna do when your loop has finished
}
}
You can call it like this new YourTaskClassName().execute();
Upvotes: 1
Reputation: 8138
It depends on where do you have the loop. If you run the loop in main thread, you can't "simply insert a delay" into it, because it will block execution, and Java doesn't have anything like C#'s async
&await
to "easily" solve this. So, the easiest way to do this is: first, move the entire loop to a background thread. Then, you can just use Thread.sleep(…)
where you need a delay. But then, if you need to update UI, you can't do this directly from background thread, you will need to use a Handler, call post(Runnable)
method (the passed Runnable will run on main thread), and inside that Runnable you must check if the UI is still alive (because user could "close" the application, so your Activity/Fragment/View/whatever can be finished or be in a "bad" state)
Upvotes: 2
Reputation: 556
I've done Android programming in the past but not recently though. I have done lots of Java though and think that I will still be of help.
With respect to the Handler, it appears that you would be able to do this. Having looked at the documentation, http://developer.android.com/reference/android/os/Handler.html#handleMessage(android.os.Message), essentially you're posting messages or runnables from one thread and handling messages in a different thread. Your for-loop won't stop though since you're starting a new thread when using a handler.
When creating a handler, you need to override the handleMessage(Message msg) method if it's a message or post a runnable you're sending as this is the method that's called after a suitable time has ellapsed. To send your message at a specific time or after a delayed amount of time, you have postAtTime, postDelayed, sendMessageAtTime and sendMessageDelayed methods, whichever one is needed.
new Handler() {
public void handleMessage(Message msg) {
// Your code here.
}
}.sendMessageDelayed(yourMessage, theAmountOfTimeInMilles);
Also, after your message is handled, if you want to do any user-interface updating (in other words, changing anything graphical, such as updating a label or changing the background), you need to use the runOnUiThread method, otherwise it will throw an exception:
runOnUiThread(new Runnable() {
public void run() {
// Code including UI code here.
}
});
Upvotes: 0
Reputation: 329
Wait is a monitor method which means you can call the method from synchronized block or method. Use sleep for your case.
Upvotes: 0
Reputation: 3419
Thread.sleep(time_in_miliseconds)
looks like the simplest solution. It's a static method, so you need no instances of Thread class.
Upvotes: 0