oreo中的sendOrderedBroadcast setPackage要求 [英] sendOrderedBroadcast setPackage requirement in Oreo
问题描述
为什么以下 Ordered Broadcast 在Android Oreo中失败,除非我具体设置包名?
Why would the following Ordered Broadcast fail in Android Oreo, unless I specifically set the package name?
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
将会失踪。
If the package name is not set, EXTRA_SUPPORTED_LANGUAGES
will be missing.
我最近问了一个赏金问题我没有设置包名的'遗留代码'在Oreo中失败,但在以前的Android版本上成功运行。
I recently asked a bounty question where my 'legacy code', which didn't set the package name, was failing in Oreo, but worked successfully on previous Android versions.
检查了所有的<一个href =https://developer.android.com/about/versions/oreo/android-8.0-changes.html =nofollow noreferrer> API 26中的行为变化我什么都看不到解释一下。
Having checked all of the behavioural changes in API 26 I can see nothing that would explain this.
有人可以解释可能的原因吗?
Can anyone shed some light on the possible cause please?
注意:示例代码和问题假设设备有 Google的现在应用程序,以提供 RecognitionService
Note: The sample code and issue assumes the device has Google's 'Now' application installed to provide the RecognitionService
推荐答案
好的,我转载了这个问题。 1234
结果代码是一个红色的鲱鱼—看起来 RecognizerIntent
背后的过程没有设置结果代码,所以你得到了初始代码。
OK, I reproduced the problem. The 1234
result code was a red herring — it looks like the process behind RecognizerIntent
does not set a result code, so you get the initial code.
但是,你确实在Android 8.1(并且,可能是8.0)上收到此错误消息:
However, you do get this error message on Android 8.1 (and, presumably, 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
这就是你在清单中注册了一个接收者,我们不打算给你广播,因为你在后台错误。
That's the "you registered a receiver in the manifest, and we're not going to give you the broadcast, because you are in the background" error.
这个经过轻度测试的 sendImplicitOrderedBroadcast()
方法解决了这个问题,同时原则上维护了接收器的顺序(降序为优先权):
This lightly-tested sendImplicitOrderedBroadcast()
method works around the problem, while in principle maintaining the order of receivers (descending by priority):
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);
}
}
我冒昧提交问题。
这篇关于oreo中的sendOrderedBroadcast setPackage要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!