Oreo 中的 sendOrderedBroadcast setPackage 要求
sendOrderedBroadcast setPackage requirement in Oreo
为什么以下 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
会丢失
我最近 我的 'legacy code' 没有设置包名称,在 Oreo 中失败了,但在以前的 Android 版本上运行成功。
检查了所有 behavioural changes in API 26 我看不出任何可以解释这一点的东西。
任何人都可以阐明可能的原因吗?
注意:示例代码和问题假定设备具有 Google's 'Now' application installed to provide the RecognitionService
好的,我重现了问题。 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
那是 "you registered a receiver in the manifest, and we're not going to give you the broadcast, because you are in the background" 错误。
这个经过简单测试的 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);
}
}
为什么以下 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
会丢失
我最近
检查了所有 behavioural changes in API 26 我看不出任何可以解释这一点的东西。
任何人都可以阐明可能的原因吗?
注意:示例代码和问题假定设备具有 Google's 'Now' application installed to provide the RecognitionService
好的,我重现了问题。 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
那是 "you registered a receiver in the manifest, and we're not going to give you the broadcast, because you are in the background" 错误。
这个经过简单测试的 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);
}
}