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
Your current call is:
Here,
chooser
is anIntent
, andMATCH_DEFAULT_ONLY
is anint
.That matches the deprecated
queryIntentActivities()
version.On API Level 33 and higher devices, Google would like you to use the
queryIntentActivities()
version that takes aResolveInfoFlags
as the second parameter, instead of anint
. You would useResolveInfoFlags.of()
to wrapMATCH_DEFAULT_ONLY
into aResolveInfoFlags
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, orUse
Build.VERSION.SDK_INT
to determine the API level and call the desired version ofqueryIntentActivities()
based on the API level of the deviceThis 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
I think we can cast PackageManager.MATCH_DEFAULT_ONLY into long.
Something like this: