【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);
        }
    }
}
相关推荐
二川bro1 小时前
Python | TypeError: unsupported operand type(s) for +=: ‘int’ and ‘str’
android·java·python
Tʀᴜsᴛ⁴¹⁷4871 小时前
实训day22(8.6)
android·adb
九霄云客2 小时前
SuccBI+低代码文档中心 — 低代码应用(SuperPage)(上)(重点)
android·低代码·rxjava
worker..3 小时前
[MRCTF2020]Ezpop1
android
坐望云起3 小时前
如何在 Android 设备上更改您的位置?
android·gps·定位·fake location
.try-3 小时前
js与ios、安卓原生方法互调。
android·开发语言·javascript
姜君竹4 小时前
安卓碎片Fragment
android·java·开发语言·学习·ui
程序员不想YY啊6 小时前
【保姆级讲解C语言中的运算符的优先级!】
android·c语言·开发语言
Geeker556 小时前
恢复已删除文件工具之11 个最佳恢复文件工具
android·大数据·数据库·学习·pdf·电脑·笔记本电脑
叫我龙翔8 小时前
【C++】C++11的新特性 — function 包装器 , bind包装器
android·java·数据结构·c++·算法·学习方法