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>

完成

相关推荐
哑巴湖小水怪6 小时前
Android的架构是四层还是五层
android·架构
2501_916008898 小时前
深入解析iOS应用启动性能优化策略与实践
android·ios·性能优化·小程序·uni-app·cocoa·iphone
美狐美颜SDK开放平台9 小时前
短视频/直播双场景美颜SDK开发方案:接入、功能、架构详解
android·ios·美颜sdk·第三方美颜sdk·视频美颜sdk
untE EADO10 小时前
在 MySQL 中使用 `REPLACE` 函数
android·数据库·mysql
iblade10 小时前
Android CLI And Skills 3x faster
android
阿巴斯甜12 小时前
SharedUnPeekLiveData和UnPeekBus的区别:
android
阿巴斯甜12 小时前
UnPeek-LiveData的使用:
android
我就是马云飞12 小时前
我废了!大厂10年的我面了20家公司,面试官让我回去等通知!
android·前端·程序员
limuyang213 小时前
在 Android 上用上原生的 xxHash,性能直接拉满
android
Fate_I_C14 小时前
ViewModel 的生命周期与数据保持
android·kotlin