Android 开机自启动

APP需要开机自启动,要通过开机广播实现。

1,在AndroidManifest.xml中增加权限

复制代码
    <!-- .接收启动完成的广播权限 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2,在AndroidManifest.xml中application标签内增加开机广播

复制代码
     <receiver
            android:name=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">

            <!--接收启动完成的广播-->
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>

3,增加开机广播实现类,其中MainActivity.class是开机启动页面

复制代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            Intent thisIntent = new Intent(context, MainActivity.class);
            thisIntent.setAction("android.intent.action.MAIN");
            thisIntent.addCategory("android.intent.category.LAUNCHER");
            thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(thisIntent);
        }
    }
}
相关推荐
AI2中文网1 小时前
Ai2 Starter 新版上线:Android 15、Hyper-V 与更快的模拟器体验
android
挂科边缘1 小时前
Android Studio 保姆级安装教程,Windows 电脑上安装 Android Studio
android·windows·android studio
Danny_姜2 小时前
Android Studio Quail 4 稳定版发布:本地 Gemma 4 + Android Skills
android·ide·android studio
平头哥AI11 小时前
Day 22 _ 包装错误别丢链_%w、errors.Is 与 errors.As
android·服务器·学习·golang·go
聚美智数12 小时前
图片广告检测-图片审核-图片广告识别-图像广告检测
android
早睡早起身体好12315 小时前
用 XGrammar 约束大模型工具调用:解决参数为空的问题
android·人工智能·神经网络·机器学习·自然语言处理·vllm
大佐不会说日语~16 小时前
Android 16 下 uni-app APP 提示“网络连接失败”的排障与修复
android·uni-app
2501_9151063218 小时前
苹果App Store上架费用及流程全面解析
android·ios·小程序·https·uni-app·iphone·webview
YF021119 小时前
Android 16系统App开机自启动方案
android
hm宋20 小时前
安卓手机卡顿优化 —— 背景与使用指南
android