用于BOOT_COMPLETED的BroadcastReceiver太慢了

6 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>
Run Code Online (Sandbox Code Playgroud)

广播接收器如下:

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

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

Com*_*are 8

重启后有没有什么可以立即启动应用程序?

没有.

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

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


小智 5

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

<intent-filter android:priority="999">
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
Run Code Online (Sandbox Code Playgroud)

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

关于优先级的摘录:

它控制执行广播接收器以接收广播消息的顺序。具有较高优先级值的那些在具有较低值的那些之前被调用。(该顺序仅适用于同步消息;异步消息会忽略它。)

仅当您确实需要强加接收广播的特定顺序,或者想要强制 Android 优先选择一个活动而不是其他活动时,才使用此属性。

该值必须是整数,例如“100”。更高的数字具有更高的优先级。默认值为 0。该值必须大于 -1000 且小于 1000。

  • 我尝试更改更新优先级,但结果没有变化。主屏幕实现有效。感谢您提供的信息,因为我从来不知道优先级,因为我是 android 的新手。 (3认同)
  • 我发现 android:priority="999" 需要放在 &lt;intent-filter&gt; 标签而不是 &lt;action&gt; 标签内。这对我有用。 (3认同)