About the Pollingservice timed polling task in Android

Source: Internet
Author: User

1. Create a Pollingthread class and print "New message!" in log

class extends Thread {        @Override        publicvoid  run () {            log.i ("Zzf", "Polling ...") );        }    }

2. Create Pollingreceiver to receive service delivery information and re-execute Pollingservice

 Public class extends broadcastreceiver {    @Override    publicvoid  onreceive (context context, Intent Intent) {        //  TODO auto-generated method stub        log.i ("Zzf", "receive!" );         New Intent (context, Pollingservice.  Class);        Context.startservice (i);    }}

3. Create a Pollingservice timed poll task object

 Public classPollingserviceextendsService {@Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {} @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {        //TODO auto-generated Method Stub//Create a Pollingthread child thread for performing tasks        NewPollingthread (). Start (); //Get Alarmmanager System ServicesAlarmmanager Manager =(Alarmmanager) Getsystemservice (Context.alarm_service); //packaging needs to perform service intentIntent i =NewIntent ( This, Pollingreceiver.class); Pendingintent pendingintent= Pendingintent.getbroadcast ( This, 0, I, 0); intAnseconds = 5 * 1000;//5 Seconds//Trigger Time        LongTriggerattime = Systemclock.elapsedrealtime () +Anseconds;        Manager.set (Alarmmanager.elapsed_realtime_wakeup, Triggerattime, pendingintent); return Super. Onstartcommand (I, Flags, startid); }        

4. Start the Pollingservice in mainactivity

LOG.I ("Zzf", "Start polling service ...");         New Intent (This, pollingservice.  Class);        StartService (intent);

About the Pollingservice timed polling task in Android

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.