用于BOOT_COMPLETED的BroadcastReceiver太慢了

时间:2017-05-15 20:48:45

标签: android broadcastreceiver bootcompleted

以下是我的清单文件。

    <?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>

广播接收器如下:

    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);
    }
}

}

没有错误。该应用程序正在重启电话时打开。但重启后启动应用程序需要一分钟的时间。重启后是否有什么可以立即启动应用程序?

2 个答案:

答案 0 :(得分:6)

  

重启后是否有立即启动应用程序的内容?

没有

有许多应用程序希望在启动时获得控制权。你的转弯速度将取决于许多变量,例如安装的应用程序数量,设备的CPU速度,设备上的系统RAM数量等。

此外,在启动时从BroadcastReceiver开始活动是相当邪恶的。如果您希望成为用户在重新启动后看到的第一件事,请编写主屏幕实现。

答案 1 :(得分:2)

首先需要启动一些系统资源,并且优先级高于接收器。但是,您可以尝试在清单中设置您的意图优先级。像这样:

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

请查看开发者文档中有关此内容的详细信息: Docs

提取优先顺序: 它控制广播接收器执行接收广播消息的顺序。具有较高优先级值的那些在具有较低值的那些之前被调用。 (该命令仅适用于同步消息;对于异步消息,它会被忽略。) 仅当您确实需要强制接收广播的特定订单,或者希望强制Android更喜欢一项活动而不是其他活动时,才使用此属性。 该值必须是整数,例如&#34; 100&#34;。数字越大,优先级越高。默认值为0.该值必须大于-1000且小于1000。