BroadcastReceiver for BOOT_COMPLETED is too slow

人走茶凉 提交于 2019-12-06 02:04:17

问题


The below is my manifest file.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mccheekati.test_trail">
    <uses-permission 
    android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

   <receiver 
   android:name="com.example.mccheekati.test_trail.yourActivityRunOnStartup"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" 
           />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The Broadcast receiver is as follows:

    public class yourActivityRunOnStartup extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

}

No errors. The application is opening on rebooting the phone. But it takes a minute's time to launch the application after reboot. Is there any what to start the application immediately after reboot?


回答1:


Is there any what to start the application immediately after reboot?

No.

There are many, many apps that want to get control at boot time. How quickly yours will get its turn will depend on many variables, such as the number of installed apps, the CPU speed of the device, the amount of system RAM on the device, etc.

Also, starting an activity from a BroadcastReceiver at boot time is fairly evil. If you want to be the first thing the user sees after a reboot, write a home screen implementation.




回答2:


There will be some system resources that need to boot first and will have a higher priority over your receiver. However you could try setting a priority to your intent in the manifest. Like so:

<intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" android:priority="999"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" 
       />

Please have a look at the details from the developer docs regarding this: Docs

Extract regarding priority: It controls the order in which broadcast receivers are executed to receive broadcast messages. Those with higher priority values are called before those with lower values. (The order applies only to synchronous messages; it's ignored for asynchronous messages.) Use this attribute only if you really need to impose a specific order in which the broadcasts are received, or want to force Android to prefer one activity over others. The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.



来源:https://stackoverflow.com/questions/43988566/broadcastreceiver-for-boot-completed-is-too-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!