The implementation method of the Android boot self service _android

Source: Internet
Author: User

Android is probably the simplest of the mobile operating system, and we just need to listen to a boot-initiated broadcast (broadcast). First write a receiver (i.e. broadcast listener) and inherit Broadcastreceiver as follows:

View Source

Print?

Copy Code code as follows:

public class Bootreceiver extends Broadcastreceiver {

Private Pendingintent Malarmsender;

@Override

public void OnReceive (context context, Intent Intent) {

Do what you want to do here (start a service,activity, etc.), this example is to start a timer scheduler, start a service every 30 minutes to update the data

Malarmsender = Pendingintent.getservice (context, 0, new Intent) (context,

Refreshdataservice.class), 0);

Long firsttime = Systemclock.elapsedrealtime ();

Alarmmanager am = (alarmmanager) context

Getsystemservice (Activity.alarm_service);

Am.cancel (Malarmsender);

Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime,

* 1000, Malarmsender);

}

}


Next, we only need to register this receiver in the application configuration file Androidmanifest.xml to listen for system startup events, as follows:

View Source

Print?

Copy Code code as follows:

< receiver Android:name= ". Service. Bootreceiver ">

< intent-filter>

<!--call--> after system startup completes

< action android:name= "Android.intent.action.BOOT_COMPLETED" >

</action>

</intent-filter>

</receiver>

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.