Android >= Api 26 中未调用 BroadcastReceiver
BroadcastReceiver is not called in Android >= Api 26
BroadcastReceiver 在 Api 26 或更高版本中无法执行任何操作,根据文档,隐式操作已被阻止,但 boot_completed 不会。我用 Android 19、24、26 和 28 进行了测试。
这是我的收件人声明:
<receiver
android:name="de.com.limto.limto1.broadcastReceiver"
android:label="StartMyServiceAtBootReceiver"
android:directBootAware="true"
android:enabled="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
这是我的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
这是我的接收器:
@Override
public void onReceive(Context context, Intent intent) {
try {
Log.d(BROADCAST_RECEIVER,intent.getAction());
lib.appendLog(intent.getAction(),context);
lib.ShowToast(context, BROADCAST);
if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
if (service != null) {
try {
service.stop();
} catch (Throwable throwable) {
throwable.printStackTrace();
Log.e(BROADCAST_RECEIVER,throwable.getMessage(), throwable);
}
}
} else {
/*if (service != null) return;
Intent serviceIntent;
serviceIntent = new Intent(context, LimindoService.class);
serviceIntent.putExtra(BOOT, true);
*/
LimindoService.ccontext = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//context.startService(serviceIntent);
lib.scheduleJob(context);
} else {
//context.startService(serviceIntent);
startServiceByAlarm(context);
}
}
}
catch (Throwable ex)
{
ex.printStackTrace();
lib.appendLog(ex.getMessage(), context);
Log.e(BROADCAST_RECEIVER,null,ex);
lib.ShowToast(context, ex.getMessage());
}
}
AFAIK BOOT_COMPLETED
广播没有类别 android.intent.category.DEFAULT
,删除类别应该可以解决问题。
此行为仅在 api >= 26 的某些设备上发生...这是由 phone 设置引起的:设置中禁用了启动时启动服务。
BroadcastReceiver 在 Api 26 或更高版本中无法执行任何操作,根据文档,隐式操作已被阻止,但 boot_completed 不会。我用 Android 19、24、26 和 28 进行了测试。
这是我的收件人声明:
<receiver
android:name="de.com.limto.limto1.broadcastReceiver"
android:label="StartMyServiceAtBootReceiver"
android:directBootAware="true"
android:enabled="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
这是我的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
这是我的接收器:
@Override
public void onReceive(Context context, Intent intent) {
try {
Log.d(BROADCAST_RECEIVER,intent.getAction());
lib.appendLog(intent.getAction(),context);
lib.ShowToast(context, BROADCAST);
if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
if (service != null) {
try {
service.stop();
} catch (Throwable throwable) {
throwable.printStackTrace();
Log.e(BROADCAST_RECEIVER,throwable.getMessage(), throwable);
}
}
} else {
/*if (service != null) return;
Intent serviceIntent;
serviceIntent = new Intent(context, LimindoService.class);
serviceIntent.putExtra(BOOT, true);
*/
LimindoService.ccontext = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//context.startService(serviceIntent);
lib.scheduleJob(context);
} else {
//context.startService(serviceIntent);
startServiceByAlarm(context);
}
}
}
catch (Throwable ex)
{
ex.printStackTrace();
lib.appendLog(ex.getMessage(), context);
Log.e(BROADCAST_RECEIVER,null,ex);
lib.ShowToast(context, ex.getMessage());
}
}
AFAIK BOOT_COMPLETED
广播没有类别 android.intent.category.DEFAULT
,删除类别应该可以解决问题。
此行为仅在 api >= 26 的某些设备上发生...这是由 phone 设置引起的:设置中禁用了启动时启动服务。