1.manifest里注册
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name=".BootCompleteReceiver" android:exported="true"> <intent-filter android:priority="30000"> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
2.监听
public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { //开机启动完成后,要做的事情 Log.e(getClass().getName(), "BootBroadcastReceiver onReceive(), Do thing!"); context.startForegroundService(new Intent(context,MyService.class)); } } }