1.首先继承一个broadcastreceiver
public class ConnectBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ Intent bootActivityIntent=new Intent(context,MainActivity.class); bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(MainActivity);//要启动应用程序的首界面 } } }
2.在AndroidMenifest.xml中配置Receiver
<receiver android:name=".BootBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver>
3.添加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>