1.android Self-timer task with alarm
Android Alarm clock can be used in conjunction with the broadcast (not recommended), system resources wasted, the Android system after 5.0 timing
Task seems to trigger time is not allowed, because to save electricity.
//get system alarm Alarmmanager Alarmmanager = (alarmmanager) getsystemservice (ALARM_ SERVICE) ;intent Intent = new Intent (reportdetailsactivity .this, Reportdetailsactivity.class) ;p endingintent = PendingIntent.getbroadcast (Getapplicationcontext (), 0, intent, 0) ;//start the scheduled task Alarmmanager.setRepeating ( Alarmmanager.currenttimemillis () + 1000, 5 * 1000, pendingintent)
Remember to configure the broadcast in the Manifeast file
public static class myreceiver extends broadcastreceiver {@ Override public void onReceive ( Context context, Intent Intent) {if (Bo > 0) {if (Bo > 240) {//Brush ticket handler.sendemptymessage ( Span class= "Hljs-number" >3); //window warning swipe ticket} else {handler.sendemptymessage (2);}}} }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
To cancel an alarm in OnDestroy ()
@Overrideprotected void onDestroy() { alarmManager.cancel(pendingIntent);}
2. Turn on thread
Sleep in 5s to schedule operation tasks.
class Myrunnable implements runnable{@Override public void Run () {while (isloop) {try {if (Bo > 0) {if (Bo > 240) {//Brush ticket handler.sendemptymessage (3); //pop-up window warning Brush ticket} else {handler.sendemptymessage ( 2); }} thread.sleep (5000);} catch (exception e) {e.printstacktrace ();}}} }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
In the OnCreate () method, open:
loopThread=new Thread(new MyRunnable());loopThread.start();
Terminates the thread when the page is destroyed
isLoop=false;loopThread.interrupt();
3. Use the Timer class.
Turn on timer
Timer timer=new Timer(); timer.schedule(new TimerTask() { @Override public void run() { ... } },new Date(),5000);
Stop Timer
timer.cancel();
The above three kinds of scheduled tasks in addition to the first do not use, we recommend the use of third and second.
Java + Android timed tasks