使用广播实现
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);
}
}
}
Informações:
- Autor:Alex Yucra
- Link:https://alexyucra.github.io/blog/wiki/android-auto-boot/
- Declaração de direitos autorais: atribuição gratuita de reimpressão-não-comercial-não-derivada-manter(Licença Creative Commons 3.0)