4

为什么以下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会丢失。

我最近问了一个赏金问题,我没有设置包名称的“遗留代码”在 Oreo 中失败,但在以前的 Android 版本上成功运行。

在检查了API 26 中的所有行为变化后,我看不出任何可以解释这一点的东西。

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

注意:示例代码和问题假设设备安装了Google 的“Now”应用程序以提供RecognitionService

4

1 回答 1

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

那是“你在清单中注册了一个接收器,我们不会给你广播,因为你在后台”错误。

这种经过简单测试的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);
    }
  }

我冒昧地提出了一个问题

于 2018-02-08T00:40:24.580 回答