Android如何实现开机自启

开机自启有很多种办法,下面用广播的方式实现。

1、首先先创建广播,开机代码

java 复制代码
/**
 * Created by Forrest.
 * User: Administrator
 * Date: 2023/3/6
 * Description:
 */
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);
        }
    }
}

2、在清单文件里面注册,同时添加权限

java 复制代码
	<!-- .开机自启广播 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
		<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>

完成

相关推荐
jingling55517 小时前
Flutter | Dio网络请求实战
android·开发语言·前端·flutter
帅次17 小时前
讯飞与腾讯云:Android 实时语音识别服务对比选择
android·ios·微信小程序·小程序·android studio·android runtime
jiayong2318 小时前
MySQL 排序规则冲突问题与 utf8mb4_general_ci 统一方案
android·mysql·ci/cd
随遇丿而安19 小时前
第6周:RecyclerView 真正难的不是“写个列表”,而是让列表在复用中保持正确
android
晓梦林19 小时前
EVA靶场学习笔记
android·笔记·学习
私人珍藏库19 小时前
【Android】抖音无水印下载安卓端 轻载 QingZai v1.0.4
android·app·工具·软件·多功能
qq36219670520 小时前
Twitter官网下载安装指南:2026最新安卓版APK教程
android·twitter
盼小辉丶20 小时前
PyTorch深度学习实战(55)——在Android上部署PyTorch模型
android·pytorch·python·模型部署
ImTryCatchException20 小时前
Android 卡顿诊断实战:从“感觉卡“到“精准定位“的方法论
android
vensli20 小时前
Wolverine:杀不死的 Android 进程保活方案
android