java-Oreo中的sendOrderedBroadcast setPackage要求

除非我专门设置软件包名称,否则以下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上未设置软件包名称的“旧版代码”在Oreo中失败,但在以前的Android版本上可以成功运行.

检查了所有的behavioural changes in API 26之后,我看不到任何可以解释的信息.

任何人都可以阐明可能的原因吗?

注意:示例代码和问题假定设备已安装Google’s ‘Now’应用程序以提供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

那是“您在清单中注册了一个接收器,并且由于您处于后台状态,我们不会广播给您”错误.

这个经过严格测试的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.

转载注明原文:java-Oreo中的sendOrderedBroadcast setPackage要求 - 代码日志