为什么以下Ordered Broadcast会在Android Oreo中失败,除非我专门设置了包名?
final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
// Setting the package it will work. Omitting, it will fail
// vrIntent.setPackage("com.google.android.googlequicksearchbox");
getContext().sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
// final Bundle bundle = intent.getExtras();
final Bundle bundle = getResultExtras(true);
if (bundle != null) {
if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");
final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());
} else {
Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
}
} else {
Log.w("TAG", "onReceive: Bundle null");
}
}, null, 1234, null, null);
如果未设置包名称,则EXTRA_SUPPORTED_LANGUAGES
将丢失。
我最近asked a bounty question我的遗留代码&#39;没有设置软件包名称,在Oreo中失败,但在以前的Android版本上成功运行。
检查了所有behavioural changes in API 26后,我什么都看不到可以解释这一点。
有人可以解释可能的原因吗?
注意:示例代码和问题假定设备已安装Google's 'Now'应用程序以提供RecognitionService
答案 0 :(得分:3)
好的,我重现了这个问题。 1234
结果代码是一个红色的鲱鱼 - 看起来RecognizerIntent
背后的进程没有设置结果代码,因此您获得了初始代码。
但是,您确实在Android 8.1(并且可能是8.0)上收到此错误消息:
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.speech.action.GET_LANGUAGE_DETAILS flg=0x10 } to com.google.android.googlequicksearchbox/com.google.android.voicesearch.intentapi.IntentApiReceiver
你在清单上注册了一个接收者,我们不打算给你播放,因为你在后台&#34;错误。
这种经过轻度测试的sendImplicitOrderedBroadcast()
方法解决了这个问题,同时原则上维护了接收器的顺序(按优先级递减):
private void sendImplicitOrderedBroadcast(Intent i, String receiverPermission,
BroadcastReceiver resultReceiver,
Handler scheduler, int initialCode,
String initialData,
Bundle initialExtras) {
PackageManager pm=getPackageManager();
List<ResolveInfo> matches=pm.queryBroadcastReceivers(i, 0);
Collections.sort(matches,
(left, right) -> right.filter.getPriority()-left.filter.getPriority());
for (ResolveInfo resolveInfo : matches) {
Intent explicit=new Intent(i);
ComponentName cn=
new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
resolveInfo.activityInfo.name);
explicit.setComponent(cn);
sendOrderedBroadcast(explicit, receiverPermission, resultReceiver,
scheduler, initialCode, initialData, initialExtras);
}
}
我冒了filing an issue。