原理:
一,注册一个broadcast receiver监听action为"android.intent.action.BOOT_COMPLETED"的intent
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".BootReceiver" >
<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter></receiver>
二,broadcast receiver代码中启动activity或service
public class BootReceiver extends BroadcastReceiver {
@Overridepublic void onReceive(Context context, Intent intent) { if(intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED )){ Intent in = new Intent(context, MyActivity.class); in.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK ); context.startActivity(in); }}
}