使用广播实现
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);
}
}
}
文档信息
- 本文作者:王玉镇wangyuzhen
- 本文链接:https://wangyuzhen666.github.io//wiki/android-auto-boot/
- 版权声明:Creative Commons 4.0 传播此文章时,您需要遵守知识共享协议。 署名(BY)&非商业性(NC)&相同方式共享(SA)------------------------------- When disseminating this article, you need to adhere to a Creative Commons license. Attribution (BY) & Non-Commercial (NC) & Shareby Manner (SA) .(Creative Commons 4.0)