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>

完成

相关推荐
常利兵23 分钟前
Android 开发秘籍:用Tint为Icon动态变色
android
奔跑吧 android1 小时前
【车载audio】【CarAudioService 05】【车载 Android 系统调试深度指南:解析 dumpsys car_service】
android·audio·audioflinger·aosp15·车载音频·车载audio·car_service
shuangrenlong1 小时前
androidstudio gradle文件报红
android
Digitally1 小时前
如何通过蓝牙将 iPhone 上的照片传输到 Android
android·ios·iphone
常利兵1 小时前
Android Intent.setAction失效报错排查与修复全方案
android
低调小一1 小时前
RecyclerView 缓存与复用机制:从一次滑动讲明白(2026 版)
android·recyclerview
耶叶1 小时前
kotlin的修饰符
android·开发语言·kotlin
l1t1 小时前
在Android设备上利用Termux安装llama.cpp并启动webui
android·llama
浩宇软件开发1 小时前
基于Android天气预报应用开发APP
android·java·android studio·android开发
毕设源码-郭学长1 小时前
【开题答辩全过程】以 基于Android的电子日记APP的设计与实现为例,包含答辩的问题和答案
android