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>

完成

相关推荐
恋猫de小郭13 分钟前
React Native 鸿蒙 2026 路线发布,为什么它的适配成本那么高?
android·前端·react native
studyForMokey1 小时前
【Android面试】窗口机制专题
android·面试·职场和发展
用户013201436032 小时前
Android 资源管理与常用布局详解|基础入门
android
陆业聪2 小时前
从 OpenClaw 到 Android:Harness Engineering 是怎么让 Agent 变得可用的
android·人工智能·ai编程
stevenzqzq4 小时前
颜色透明度转换技术文档(Android/Compose)
android
巴黎没有摩天轮Li5 小时前
Android JVMTI 接入流程
android
2501_915909065 小时前
iOS 抓包不越狱,代理抓包 和 数据线直连抓包两种实现方式
android·ios·小程序·https·uni-app·iphone·webview
城东米粉儿6 小时前
Android VCL 和 NAL笔记
android
常利兵6 小时前
从0到1,解锁Android WebView混合开发新姿势
android·华为·harmonyos
背包客(wyq)6 小时前
基于Android手机的语音数据采集系统(语音数据自动上传至电脑端)
android·网络