skip to Main Content

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…

4

Answers


  1. 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

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
  3. I think we can cast PackageManager.MATCH_DEFAULT_ONLY into long.
    Something like this:

    queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
    
    Login or Signup to reply.
  4. 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
            )
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search