The methods of delay execution include:
1, open a new thread, 2, the use of timers, 3, Message processing, 4, the use of Alarmmanager
First, open a new thread
New Thread (New Runnable () {
public void Run () {
Thread.Sleep (XXXX);
Handler.sendmessage (); Tell the main thread to perform a task
}
}). Start
Second, the use of timers
TimerTask task = new TimerTask () {
public void Run () {
Execute the Task
}
};
Timer timer = new timer ();
Timer.schedule (task, delay);
Third, message processing
New Handler (). postdelayed (New Runnable () {
public void Run () {
Execute the Task
}
}, delay);
Four, Alarmmanager, simple timing generally do not use this method
Alarmmanager, as the name implies, is a "reminder", a system-level cue service commonly used in Android, that broadcasts a specified intent for us at specific times. The simple thing is that we set a time, and then at that time, Alarmmanager broadcast a intent we set for us, usually we use pendingintent,pendingintent can be understood as intent package, The simple thing is to add a specified action on the intent. When using intent, we also need to perform startactivity, StartService, or sendbroadcast to make intent useful. And Pendingintent's words are to include this action inside.
Several methods of delay execution in Android