Home > Enterprise >  How to solve queryIntentActivities deprecated in API 33
How to solve queryIntentActivities deprecated in API 33

Time:09-10

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

CodePudding user response:

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

CodePudding user response:

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)
  • Related