Java + Android timed tasks

Source: Internet
Author: User

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)     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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);}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5
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();
    • 1
    • 2
    • 1
    • 2

Terminates the thread when the page is destroyed

isLoop=false;loopThread.interrupt();
    • 1
    • 2
    • 1
    • 2
3. Use the Timer class.

Turn on timer

 Timer timer=new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {            ...            }        },new Date(),5000);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

Stop Timer

  timer.cancel();
    • 1
    • 1

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.