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>

完成

相关推荐
alexhilton10 小时前
面向开发者的系统设计:像建筑师一样思考
android·kotlin·android jetpack
CYRUS_STUDIO19 小时前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
CYRUS_STUDIO19 小时前
Frida 实战:Android JNI 数组 (jobjectArray) 操作全流程解析
android·逆向
用户091 天前
Gradle Cache Entries 深度探索
android·java·kotlin
循环不息优化不止1 天前
安卓 View 绘制机制深度解析
android
叽哥1 天前
Kotlin学习第 9 课:Kotlin 实战应用:从案例到项目
android·java·kotlin
雨白1 天前
Java 线程通信基础:interrupt、wait 和 notifyAll 详解
android·java
诺诺Okami2 天前
Android Framework-Launcher-UI和组件
android
潘潘潘2 天前
Android线程间通信机制Handler介绍
android
潘潘潘2 天前
Android动态链接库So的加载
android