BOOT_COMPLETED 的 BroadcastReceiver 太慢
BroadcastReceiver for BOOT_COMPLETED is too slow
下面是我的清单文件。
<?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>
Broadcast receiver如下:
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);
}
}
}
没有错误。应用程序在重新启动 phone 时打开。但是重启后需要一分钟的时间来启动应用程序。有什么重启后立即启动应用程序吗?
Is there any what to start the application immediately after reboot?
没有
有很多应用程序希望在启动时获得控制权。轮到您的速度取决于许多变量,例如已安装的应用程序数量、CPU 设备速度、设备上的系统 RAM 容量等。
此外,在启动时从 BroadcastReceiver
启动 activity 是相当邪恶的。如果你想成为用户在重启后看到的第一件事,写一个主屏幕实现。
将有一些系统资源需要先启动,并且比您的接收器具有更高的优先级。但是,您可以尝试在清单中为您的意图设置优先级。像这样:
<intent-filter android:priority="999">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
请查看开发者文档中关于此的详细信息:
Docs
关于优先级的摘录:
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.
下面是我的清单文件。
<?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>
Broadcast receiver如下:
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);
}
}
}
没有错误。应用程序在重新启动 phone 时打开。但是重启后需要一分钟的时间来启动应用程序。有什么重启后立即启动应用程序吗?
Is there any what to start the application immediately after reboot?
没有
有很多应用程序希望在启动时获得控制权。轮到您的速度取决于许多变量,例如已安装的应用程序数量、CPU 设备速度、设备上的系统 RAM 容量等。
此外,在启动时从 BroadcastReceiver
启动 activity 是相当邪恶的。如果你想成为用户在重启后看到的第一件事,写一个主屏幕实现。
将有一些系统资源需要先启动,并且比您的接收器具有更高的优先级。但是,您可以尝试在清单中为您的意图设置优先级。像这样:
<intent-filter android:priority="999">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
请查看开发者文档中关于此的详细信息: Docs
关于优先级的摘录:
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.