Android App boot automatically

Source: Internet
Author: User

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:
<receiverandroid: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"/>

Android App boot automatically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.