环境配置
- jdk1.8
- compileSdk 34
- minSdk 31
- targetSdk 33
代码
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application>
<receiver
android:name=".receiver.BootBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
自定义BootBroadcastReceiver(开机广播接收)
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Toast.makeText(context, "QZ-APP接收到开机广播", Toast.LENGTH_LONG);
Log.i("BootBroadcastReceiver", "QZ-APP接收到开机广播");
Intent newIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
}
}
}