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>

完成

相关推荐
Android系统攻城狮1 小时前
Android tinyalsa深度解析之pcm_state调用流程与实战(一百一十七)
android·pcm·tinyalsa·音频进阶·音频性能实战
吴声子夜歌2 小时前
RxJava——调度器Scheduler
android·echarts·rxjava
冬奇Lab3 小时前
AMS核心机制:Activity生命周期与进程管理深度解析
android·源码阅读
西邮彭于晏4 小时前
安卓app发布
android
游戏开发爱好者85 小时前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
YIN_尹6 小时前
【MySQL】SQL里的“连连看”:从笛卡尔积到自连接
android·sql·mysql
bisal(Chen Liu)6 小时前
0.5 hour还是0.5 hours?
android
特立独行的猫a6 小时前
Kuikly多端框架(KMP)实战:现代Android/KMP状态管理指南:基于StateFlow与UDF架构的实践
android·架构·harmonyos·状态管理·kmp·stateflow·kuikly
范桂飓7 小时前
Google 提示词工程最佳实践白皮书解读
android·人工智能
贤泽8 小时前
Android 15 Service 源码解析
android