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>

完成

相关推荐
JMchen12316 分钟前
高级渲染技术:OpenGL ES在自定义View中的应用
android·性能优化·3d渲染·opengl es·自定义view·glsurfaceview·shader编程
鹧鸪晏38 分钟前
搞懂 kotlin 泛型 out 和 in 关键字
android·kotlin
毕设源码-邱学长39 分钟前
【开题答辩全过程】以 基于Android的“旧时光”书店App为例,包含答辩的问题和答案
android
hashiqimiya41 分钟前
androidstudio历史版本
android
毕设源码-钟学长43 分钟前
【开题答辩全过程】以 基于Android的出租车运行监测系统设计与实现为例,包含答辩的问题和答案
android
fatiaozhang952744 分钟前
晶晨S905W2芯片_sbx_x98_plus_broagcon_atv_安卓11_线刷包固件包
android·电视盒子·刷机固件·机顶盒刷机·机顶盒刷机固件大全·晶晨s905w2芯片
匆忙拥挤repeat1 小时前
Android Compose 依赖配置解读
android
没有了遇见1 小时前
Android 关于注入Js处理Android和H5 Js 交互问题
android
阿拉斯攀登1 小时前
第 12 篇 RK 平台安卓驱动实战 5:SPI 设备驱动开发,以 SPI 屏 / Flash 为例
android·驱动开发·rk3568·瑞芯微·嵌入式驱动·安卓驱动·spi 设备驱动
Predestination王瀞潞1 小时前
Mysql忘记密码重置的方法
android·mysql·adb