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);
        }
    }
}
相关推荐
android_xc26 分钟前
Android Studio适配butterknife遇到的坑
android·ide·android studio·butterknife
2501_9159184140 分钟前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张1 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
米豆同学3 小时前
SufraceFlinger图像合成原理(3)-SurfaceFlinger中Layer的创建和销毁
android
米豆同学3 小时前
SufraceFlinger图像合成原理(2)-SurfaceFlinger与应用进程间的通信
android
用户2018792831673 小时前
uses-library:系统应用报NoClassDefFoundError问题
android
叽哥3 小时前
Kotlin学习第 4 课:Kotlin 函数:从基础定义到高阶应用
android·java·kotlin
mg6683 小时前
安卓玩机工具----安卓“搞机工具箱”最新版 控制手机的玩机工具
android·智能手机
诺诺Okami3 小时前
Android Framework- Activity启动2
android
米豆同学3 小时前
SystemUI plugin 开发
android