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);
        }
    }
}
相关推荐
-指短琴长-2 小时前
MySQL快速入门——内置函数
android·数据库·mysql
渡我白衣2 小时前
链接的迷雾:odr、弱符号与静态库的三国杀
android·java·开发语言·c++·人工智能·深度学习·神经网络
正经教主3 小时前
【App开发】02:Android Studio项目环境设置
android·ide·android studio
全栈软件开发4 小时前
最新版T5友价互站网源码商城PHP源码交易平台 完整带手机版源码网系统源码
android·开发语言·php
shykevin4 小时前
uni-app x开发商城系统,小程序发布,h5发布,安卓打包
android·小程序·uni-app
且白4 小时前
uniapp接入安卓端极光推送离线打包
android·uni-app
在狂风暴雨中奔跑4 小时前
使用MediaRecorder+MediaProjection高效实现Android录屏
android
YoungP4 小时前
让人头疼的AndroidStudio、Gradle、AGP..
android
我命由我123455 小时前
Android WebView - loadUrl 方法的长度限制
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
Coffeeee5 小时前
面试被问到Compose的副作用不会,只怪我没好好学
android·kotlin·android jetpack