Android auto-start is probably the simplest in the mobile operating system. We only need to listen to a Broadcast (Broadcast) started at the startup. First, write a Receiver (that is, a broadcast listener) that inherits BroadcastReceiver, as shown below:
View source
Print?
Copy codeThe Code is as follows: public class BootReceiver extends BroadcastReceiver {
Private PendingIntent mAlarmSender;
@ Override
Public void onReceive (Context context, Intent intent ){
// Do what you want 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,
30x60*1000, mAlarmSender );
}
}
Next, we only need to register this handler in the application configuration file AndroidManifest. xml to listen to system startup events, as shown below:
View source
Print?
Copy codeThe Code is as follows: <javaser Android: name = ". service. BootReceiver">
<Intent-filter>
<! -- Called after the system is started -->
<Action android: name = "Android. intent. action. BOOT_COMPLETED">
</Action>
</Intent-filter>
</Cycler>