【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);
        }
    }
}
相关推荐
一条上岸小咸鱼3 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto4 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆4 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren4 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.8 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
来来走走13 小时前
Flutter开发 网络请求
android·flutter
独行soc19 小时前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试
雨白20 小时前
登录和授权:Cookie与Authorization Header机制详解
android
Frank_HarmonyOS21 小时前
【Android -- 多线程】Handler 消息机制
android
一条上岸小咸鱼21 小时前
Kotlin 基本数据类型(一):概述及分类
android·kotlin