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>

完成

相关推荐
行墨17 小时前
Jetpack Compose 深入浅出(二)——基础组件Text
android
雨白18 小时前
深入理解协程的运作机制 —— 调度、挂起与性能
android·kotlin
沐怡旸19 小时前
【Android】Android系统体系结构
android
namehu19 小时前
React Native 应用性能分析与优化不完全指南
android·react native·ios
xqlily20 小时前
Kotlin:现代编程语言的革新者
android·开发语言·kotlin
HelloBan20 小时前
如何正确去掉SeekBar的Thumb和按压效果
android
木易 士心20 小时前
Android EventBus 源码解析:设计模式、原理与实现
android
ClassOps20 小时前
源码阅读 LeakCanary
android
用户20187928316720 小时前
为啥现在 Android App 不用手动搞 MultiDex 了?
android
fouryears_2341721 小时前
如何将Vue 项目转换为 Android App(使用Capacitor)
android·前端·vue.js