In Android development, the following three methods are used to perform tasks on a regular basis:
First, the use of handler and thread sleep (long) method (not recommended, Java implementation)
Second, the use of handler postdelayed (Runnable, Long) method (the simplest Android implementation)
Third, the use of handler and timer and TimerTask combined method (more tasks recommended)
Android sometimes needs to loop through a piece of code, or it needs to execute a piece of code at some point in time, and that's the first time you think of a timer object, yes, but we have a better choice.
A timer to achieve the timing of the task
Timer timer;
void OnCreate () {
...
TimerTask task = new TimerTask () {public
void run () {
//Add code to execute here
}
};
Timer = new timer ();
Timer.schedule (Task, 1000),//open timer, delay 1s execute task
}
void OnDestroy () {
...
Timer.cancel ()//Destroy Timer
}
Second, handler implementation of timed tasks
1. After a period of time to perform an operation, circular execution:
void OnCreate () {
...
Handler Handler = new Handler ();
Runnable Runnable = new Runnable () {
@Override public
void Run () {
//TODO auto-generated a stub
// Add execution Code
handler.postdelayed (this,) and 50ms to execute this, that is, Runable
}
};
Handler.postdelayed (runnable, 50);//Open timer, 50ms after runnable operation
}
void OnDestroy () {...
Handler.removecallbacks (this);//Turn off timer processing
}
2. After a period of time to perform an operation, after execution, no longer executed:
void OnCreate () {
...
Handler Handler = new Handler ();
Runnable Runnable = new Runnable () {
@Override public
void Run () {
//TODO auto-generated a stub
Add code to execute here
dosomething ();
Handler.removecallbacks (this); Remove timed Task
}
};
Handler.postdelayed (runnable, 50);//Turn on timer, 50ms execute runnable
}
Third, Alarmmanager to achieve accurate timing operation
When we use timer or handler, we find that delay time is not so accurate. If we need a strictly punctual timing operation, then use the Alarmmanager,alarmmanager object with intent use, can be timed to open an activity, send a broadcast, or open a service.
The following code describes in detail the use of two timing methods:
To perform an action after a specified length of time
The following code is a code fragment in the << soccer instant score >>. public static alarmmanagerutil{public static Alarmmanager Getalarmmanager (context ctx) {return (Alarmmanager) c
Tx.getsystemservice (Context.alarm_service); /** * Update event information after specified time (as set of alarm clock) * Note: Receiver Remember to register in Manifest.xml * * @param ctx/public static V
OID Sendupdatebroadcast (Context ctx) {log.i ("score", "Send to start update Broadcase,delay time:" +);
Larmmanager am = Getalarmmanager (CTX);
Seconds will generate a broadcast, triggering the execution of the Updatereceiver, which is the real update of the operation of the data main code Intent i = new Intent (CTX, Updatereceiver.class);
Pendingintent pendingintent = Pendingintent.getbroadcast (CTX,, I,);
Am.set (ALARMMANAGER.RTC, System.currenttimemillis () +, pendingintent)}/** * Cancel timed execution (like the cancellation of the alarm clock) * @param ctx
*/public static void Cancelupdatebroadcast (context ctx) {Alarmmanager am = Getalarmmanager (CTX);
Intent i = new Intent (CTX, Updatereceiver.class); Pendingintent pendingintent = Pendingintent.getbroadcast (CTX,, I,);
Am.cancel (pendingintent); }//Update the broadcast receiver of the database public static class Updatereceiver extends broadcastreceiver{public void onreceive (context con
Text, Intent Intent) {toast.maketext (context, "Update score Data", Toast.length_long). Show ();
Set the global timer (alarm clock) seconds before broadcasting to notify this broadcast receiver to trigger execution.
This approach is much like the settimeout (XXX,) alarmmanagerutil.sendupdatebroadcast (context) in JavaScript; }
}
Perform an action periodically
Publicstaticvoid sendupdatebroadcastrepeat (Context ctx) {
Intent Intent =new Intent (CTX, updatereceiver.class);
Pendingintent pendingintent = pendingintent.getbroadcast (ctx, 0, intent, 0);
Start time
long Firstime=systemclock.elapsedrealtime ();
Alarmmanager am = (alarmmanager) ctx.getsystemservice (alarm_service);
60 seconds a cycle, non-stop to send broadcast
am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firstime, 60*1000, pendingintent);
Cancel timer (Alarm clock)
/**
* Cancel timed execution (like the cancellation of the alarm clock)
*
* @param ctx
*/publicstaticvoid cancelupdatebroadcast (context ctx) {
Alarmmanager am = Getalarmmanager (CTX);
Note that the updatereceiver.class must be consistent with the setting, so that the
Intent i = new Intent (CTX, Updatereceiver.class) is properly canceled;
Pendingintent pendingintent = pendingintent.getbroadcast (ctx, 0, I, 0);
Am.cancel (pendingintent);
}
The above is a small set to introduce the Android timing task process detailed, I hope you like.