java - Oreo中的sendOrderedBroadcast setPackage要求

标签 java android broadcastreceiver google-voice-search

为什么以下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版本上运行成功。
在检查了所有的asked a bounty question之后,我看不出任何东西可以解释这一点。
有人能解释一下可能的原因吗?
注意:示例代码和问题假设设备安装了behavioural changes in API 26应用程序以提供Google's 'Now'

最佳答案

好吧,我重现了这个问题。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

本文翻译自 https://stackoverflow.com/questions/48653654/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。


相关文章:

android - 安排多个将来的通知

java - 两个线程进入可运行的同步方法

java - 使用正则表达式验证mysql语句

java - 是否有任何钩子可以避免在Java中用垃圾回收注册线程?

android - 即使不在前景中,如何更改导航栏背景(不仅仅是颜色)?

android - Socialauth-android获取oAuth-token并在设备上使用帐户信息

java - BroadcastReceiver for ACTION_MEDIA_BUTTON无效

java - 从动态数组将对象存储在静态数组中-Java

android - 在Android中使用套接字发送文件

android - 如何在BroadcastReceiver.onReceive中设置RecyclerView.ViewHolder的颜色?