【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);
        }
    }
}
相关推荐
郑州光合科技余经理13 分钟前
国际版外卖系统:税率字段怎么和订单主流程解耦
android·java·开发语言·前端·后端·php·ai编程
andyweike5 小时前
Android Automotive
android
又见情义5 小时前
记一次 RK3568 Android 13 蓝牙反复重启问题的排查与修复
android
hai_android6 小时前
Kotlin Flow combine 源码剖析:一个 Channel 如何优雅合并多条流
android·java·kotlin
敲代码的瓦龙7 小时前
Jetpack?ViewModel!!!
android·开发语言·kotlin·android-studio
Godikov8 小时前
Android 工控终端实战:从秒级卡顿到毫秒响应,SQLite/LitePal 性能优化全记录
android
星间都市山脉9 小时前
Android16 SystemService.onBootPhase 调用时机
android·java·linux·windows·ubuntu
swithun10 小时前
不用 WebView,我用 Kotlin + Compose Multiplatform 重写 Mermaid,并做了 2048 组对拍
android·开源·kotlin
AI吃大瓜10 小时前
人脸检测和行人检测4:Android实现YOLOv8 YOLO11 YOLO26人脸检测和人体检测(含源码,可实时检测)
android·yolo·人脸检测·人体检测·行人检测·yolo26
与海boy10 小时前
Prompt模版
android·prompt