Android 开机自启动

APP需要开机自启动,要通过开机广播实现。

1,在AndroidManifest.xml中增加权限

复制代码
    <!-- .接收启动完成的广播权限 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2,在AndroidManifest.xml中application标签内增加开机广播

复制代码
     <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>

3,增加开机广播实现类,其中MainActivity.class是开机启动页面

复制代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


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);
        }
    }
}
相关推荐
乾坤一气杀23 分钟前
Kotlin 协程线程切换原理 —— 以 Dispatchers.IO 为例
android
小书房1 小时前
Android各版本主要新特性
android
兄弟加油,别颓废了。1 小时前
ctf.show_web3
android
火柴就是我1 小时前
代码记录android怎么实现状态栏导航栏隐藏
android·flutter
梦里花开知多少2 小时前
浅谈ThreadPool
android·面试
帅次2 小时前
单例初始化中的耗时操作如何拖死主线程
android·webview·android runtime
用户0874881999172 小时前
Android 资源类型全解析及四大常用布局资源深度指南
android
火锅鸡的味道2 小时前
解决AOSP工程Android Studio打开卡顿
android·python·android studio
2501_915921433 小时前
2026 iOS 上架新趋势 iOS 发布流程模块化
android·ios·小程序·https·uni-app·iphone·webview