[FIXED] How to solve queryIntentActivities deprecated in API 33

Issue

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…

Solution

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

Answered By – CommonsWare

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published