Android App开机自动启动

来源:互联网 发布:xp如何安装网络打印机 编辑:程序博客网 时间:2024/04/26 11:31
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"/>
0 2