使用广播实现
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
... />
<receiver android:name=".receiver.AutoBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
AutoBootReceiver.java
public class AutoBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (TextUtils.isEmpty(action)) {
return;
}
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
LogUtils.i("receive boot_completed broadcast");
Intent i = new Intent(context, InitializeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
文档信息
- 本文作者:Zhuang Ma
- 本文链接:https://mazhuang.org/wiki/android-auto-boot/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)