【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);
        }
    }
}
相关推荐
二流小码农2 小时前
鸿蒙开发:DevEcoTesting中的稳定性测试
android·ios·harmonyos
一起搞IT吧2 小时前
相机Camera日志实例分析之二:相机Camx【专业模式开启直方图拍照】单帧流程日志详解
android·图像处理·数码相机
xzkyd outpaper2 小时前
Android中ContentProvider细节
android·计算机八股
恋猫de小郭2 小时前
Flutter 多版本管理工具 Puro ,它和 FVM 有什么区别?
android·前端·flutter
newki3 小时前
学习笔记,关于NDK/JNI的简介与实战
android·c++·app
zhangphil3 小时前
Android屏幕刷新率与FPS(Frames Per Second) 120hz
android
江湖有缘3 小时前
华为云Flexus+DeepSeek征文| 华为云Flexus X实例单机部署Dify-LLM应用开发平台全流程指南
android·华为云·rxjava
一杯凉白开3 小时前
硬件工程师口中的取低八位,中八位,高八位是什么意思?
android·网络协议
番茄憨憨3 小时前
Android-wifi常用接口,9个里面你看看几个对你有帮助!
android
消失的旧时光-19434 小时前
Android 开发中配置 USB 配件模式(Accessory Mode) 配件过滤器的配置
android·java