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>

完成

相关推荐
TheNextByte112 小时前
如何将Android中的照片传输到Windows 11/10
android·windows
2501_9160088912 小时前
iPhone 耗电异常检测的思路,从系统电池统计、克魔(KeyMob)、Instruments等工具出发
android·ios·小程序·uni-app·cocoa·iphone·webview
撩得Android一次心动12 小时前
Android 四大组件——ContentProvider(内容提供者)
android·内容提供者·android 四大组件
2501_9159214312 小时前
App Store 上架流程中常见的关键问题
android·ios·小程序·https·uni-app·iphone·webview
nono牛12 小时前
实战项目:设计一个智能温控服务
android·前端·网络·算法
00后程序员张1 天前
python 抓包在实际项目中的合理位置,结合代理抓包、设备侧抓包与数据流分析
android·ios·小程序·https·uni-app·iphone·webview
灵感菇_1 天前
Android Service全面解析
android·service·四大组件
alexhilton1 天前
Jetpack ViewModel内幕:内部机制与跨平台设计
android·kotlin·android jetpack
_李小白1 天前
【Android FrameWork】延伸阅读: Android应用安装过程
android
光头闪亮亮1 天前
Android手持机扫码出入库的开发详解-6.APP下载更新
android