[英]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 的某些设备上......这是由手机设置引起的:在设置中禁用了启动时启动服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.