Android(安卓)App开机自动启动
16lz
2021-01-26
Android App自动启动相关的一搜一大把,刚开始我也是从网上搜了下,不过貌似有些时候有些坑。还有就是在Flyme OS一直不能实现,后来发现是安全中心拦截了,只有白名单上面的App才能开启自启动。实现开机自启动主要是通过BroadcastReceiver接收ACTION_BOOT_COMPLETED的广播来实现的,首先新建一个BroadcastReceiver类:1、启动Activity
Intent bootIntent = new Intent(context, MainActivity.class); bootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(bootIntent);
2、启动Service
Intent bootIntent = new Intent(context, UploadImageService.class);//为了避免被强制停止后接收不到广播bootIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);context.startService(bootIntent);
3、启动整个应用
//包名为要唤醒的应用包名Intent bootIntent = context.getPackageManager().getLaunchIntentForPackage(包名);context.startActivity(bootIntent);
然后要在AndroidManifest里面注册BroadcastReceiver:
<receiver android:name=".receiver.BootReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter></receiver>
最后添加开机启动的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
更多相关文章
- create custom launcher icon 细节介绍
- android task与back stack 开发文档翻译 - 2
- 【白话】App从启动到页面显示出来发生了什么
- Android(安卓)X86 Android-x86入门之--让你的Android-x86竖屏起
- 安卓项目常规设置清单
- Android系统手机几种运行模式解析
- android系统启动及wifi框架分析
- Android启动Activity之后阻止EditText自动获得焦点
- android有序广播和无序广播的区别