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>