Android uses Broadcast (Broadcast) to enable automatic startup. androidbroadcast

Source: Internet
Author: User

Android uses Broadcast (Broadcast) to enable automatic startup. androidbroadcast

We only need to listen to a boot-up Broadcast (Broadcast.

First, write a Receiver (that is, a broadcast listener) that inherits the BroadcastReceiver, as shown below:

Public class BootReceiver extends BroadcastReceiver {private PendingIntent mAlarmSender; @ Override public void onReceive (Context context, Intent intent) {// what you want to do here (start a Service, Activity, etc ), // In this example, start a scheduled scheduling program and start a Service every 30 minutes to update 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, 30*60*1000, mAlarmSender );}}


Register the handler in the program configuration file AndroidManifest. xml to listen to system startup events.


The main reason is that you can start your own services or programs after receiving the android boot broadcast notification.

<Cycler android: name = ". service. BootReceiver"> <intent-filter> <! -- Called after the system is started --> <action android: name = "android. intent. action. BOOT_COMPLETED"> </action> </intent-filter> </Cycler>


Related Article

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.