Java – sendOrderedBroadcast setPackage requirements in Oreo

sendOrderedBroadcast setPackage requirements in Oreo… here is a solution to the problem.

sendOrderedBroadcast setPackage requirements in Oreo

Why do I have the following Ordered Broadcastin Fails 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);

If the package name is not set, EXTRA_SUPPORTED_LANGUAGES is lost.

I recently < a href="https://stackoverflow.com/q/48500077/1256219" rel="noreferrer noopener nofollow">asked a bounty question My “legacy code” didn’t set the package name, failed in Oreo, but ran successfully on previous Android versions.

All behavioural changes in API 26 were checked I don’t see anything to explain this.

Can anyone explain the possible causes?

Note: The sample code and questions assume that the device has Google’s ‘Now’ installed app offers RecognitionService

Solution

Ok, I reproduced the issue. The 1234 result code is a distraction issue — it looks like the process behind RecognizerIntent doesn’t set the result code, so you get the initial code.

However, you do get this error message on Android 8.1 (and possibly 8.0 as well:

).

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 list and we won’t broadcast to you because you’re in the background” error.

This simple test of the sendImplicitOrderedBroadcast() method solves this problem while in principle maintaining the order of recipients (in descending order of 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);
    }
  }

I take the liberty of filing an issue

Related Problems and Solutions