15

I am getting a strange behavior in Android Studio with API33. In the following code,

Intent chooser = Intent.createChooser(sharingIntent, filename);
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(chooser, android.content.pm.PackageManager.MATCH_DEFAULT_ONLY);

I am getting queryIntentActivities(Intent,int) in PackageManager has been deprecated.

In the docs, it says: This method was deprecated in API level 33. Use queryIntentActivities(android.content.Intent, android.content.pm.PackageManager.ResolveInfoFlags) instead.

I tried changing Intent with android.content.Intent, but get the same problem. PackageManager.MATCH_DEFAULT_ONLY is one of the possible flag values, so I do not understand what this error is trying to tell me...

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58

4 Answers4

24

Your current call is:

queryIntentActivities(chooser, android.content.pm.PackageManager.MATCH_DEFAULT_ONLY)

Here, chooser is an Intent, and MATCH_DEFAULT_ONLY is an int.

That matches the deprecated queryIntentActivities() version.

On API Level 33 and higher devices, Google would like you to use the queryIntentActivities() version that takes a ResolveInfoFlags as the second parameter, instead of an int. You would use ResolveInfoFlags.of() to wrap MATCH_DEFAULT_ONLY into a ResolveInfoFlags object.

That method will not be available on API Level 32 and older devices, so your choices are:

  • Stick with the int one, despite the deprecation, or

  • Use Build.VERSION.SDK_INT to determine the API level and call the desired version of queryIntentActivities() based on the API level of the device

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Great. The `ResolveInfoFlags.of()` thing is what I was missing, I didn't know about that. Yet, besides the `Build.VERSION.SDK_INT` I had to add `@SuppressWarnings("deprecation")`... – Luis A. Florit Sep 09 '22 at 19:35
10
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        context.packageManager.queryIntentActivities(
            Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER),
            PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
        )
    } else {
        context.packageManager.queryIntentActivities(
            Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER),
            PackageManager.GET_META_DATA
        )
    }
Adrian Witaszak
  • 121
  • 1
  • 5
  • Why is bottom condition deprecated? – AlexS Feb 09 '23 at 09:22
  • @AlexS The original parameters included flags, which translate to an integer. The new parameters include `ResolveInfoFlags`, which translate to a long. It honestly reads like an attempt to inline documenting what the parameter means without actually having to write documentation (Google often struggles to write competent usage documentation) – Abandoned Cart Jun 27 '23 at 11:46
1

I think we can cast PackageManager.MATCH_DEFAULT_ONLY into long. Something like this:

queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
0

This means you need to pass InfoFlag as second parameter and not an int. Here you can find list of the flags and from there you can check what each one is for so you can use it for your example So the solution would look something like

queryIntentActivities(chooser, android.content.pm.PackageManager.MATCH_DEFAULT_ONLY)
Vojin Purić
  • 2,140
  • 7
  • 9
  • 22
  • No, this is what I did already, and does not solve the problem. After all, android.content.pm.PackageManager.MATCH_DEFAULT_ONLY = 65536 is an int. – Luis A. Florit Sep 09 '22 at 19:37