Android 开机启动
创建一个Receiver,用来监听开机完毕;
public class MyReceiver extends BroadcastReceiver { static final String action_boot="android.intent.action.BOOT_COMPLETED"; public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(action_boot)){ Intent ootStartIntent=new Intent(context,MainActivity.class); ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(ootStartIntent); }else{ Log.i("ddddd====", "" + intent.getAction()); } // throw new UnsupportedOperationException("Not yet implemented"); }
一般来说开启后会启动一个Service,这里为了显示效果,启动了一个Activity。
在Mainifast.xml文件中添加相应的权限和监听
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
至此,开机启动就可以自动启动了。
注:部分安卓手机,可能会因为系统不是原装系统,无法开机启动。或者是内部有限制开机启动的程序。
自测在酷派手机上无法开机启动,华为手机上可以。暂时没有找到问题所在,大神知道的请回复。