【Android12】第三方APP开机自启

环境配置

  • jdk1.8
  • compileSdk 34
  • minSdk 31
  • targetSdk 33

代码

AndroidManifest.xml

java 复制代码
<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(开机广播接收)

java 复制代码
/**
 * 开机自启动广播接收
 */
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 = new Intent(context, MainActivity.class);  // 要启动的Activity
            //1.如果自启动APP,参数为需要自动启动的应用包名
            Intent newIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            //下面这句话必须加上才能开机自动运行app的界面
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //2.如果自启动Activity
            context.startActivity(newIntent);
            //3.如果自启动服务
//            context.startService(newIntent);
        }
    }
}
相关推荐
kymjs张涛4 小时前
OpenClaw 学习小组:初识
android·linux·人工智能
范特西林7 小时前
实战演练——从零实现一个高性能 Binder 服务
android
范特西林8 小时前
代码的生成:AIDL 编译器与 Parcel 的序列化艺术
android
范特西林8 小时前
深入内核:Binder 驱动的内存管理与事务调度
android
范特西林9 小时前
解剖麻雀:Binder 通信的整体架构全景图
android
范特西林9 小时前
破冰之旅:为什么 Android 选择了 Binder?
android
奔跑中的蜗牛66610 小时前
一次播放器架构升级:Android 直播间 ANR 下降 60%
android
测试工坊12 小时前
Android 视频播放卡顿检测——帧率之外的第二战场
android
Kapaseker14 小时前
一杯美式深入理解 data class
android·kotlin
鹏多多14 小时前
Flutter使用screenshot进行截屏和截长图以及分享保存的全流程指南
android·前端·flutter