angleUr
angleUr

Reputation: 893

PackageManager.QueryIntentActivities() does not work?

Documentation states that queryIntentActicities(Intent intent, int flags) is deprecated in API level 33, and to use this instead: https://developer.android.com/reference/android/content/pm/PackageManager#queryIntentActivities(android.content.Intent,%20android.content.pm.PackageManager.ResolveInfoFlags)

But when I use it, VS says

CS1061: 'PackageManager' does not contain a definition for 'ResolveInfoFlags' accepting a first argument of type 'PaclageManager' could be found

What am I missing?

Xamarin.Android (C#):

private bool IsThereAnAppToTakePictures()
        {
            Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
            System.Collections.Generic.IList<ResolveInfo> availableActivities = ((int)Android.OS.Build.VERSION.SdkInt > 32) ? PackageManager.QueryIntentActivities(intent, PackageManager.ResolveInfoFlags) : 
                PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
            return availableActivities != null && availableActivities.Count > 0;
        }

Upvotes: 2

Views: 2618

Answers (3)

earlypearl
earlypearl

Reputation: 596

Maybe try PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY) :

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
         resolveInfo = context.getPackageManager().resolveActivity(intentToResolve, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY));
      } else {
         resolveInfo = context.getPackageManager().resolveActivity(intentToResolve, PackageManager.MATCH_DEFAULT_ONLY);
      }

Upvotes: 0

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14509

I have checked the official document and found that the ResolveInfoFlags was added into the class Android.Content.PM.PackageManager in the Android API 33.

So you need to set the Target Framework in your project as Android 13.0 Tiramisu if you want to use this.

I updated the Visual Studio to the last version 17.3.0 Preview 5.0, and then I can use it.

But I can't find a way to get the instance of it. I tried the following code:

var resolveInfoFlags = new PackageManager.ResolveInfoFlags()
// but there is no construction method without any parameters
Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
System.Collections.Generic.IList<ResolveInfo> availableActivities = ((int)Android.OS.Build.VERSION.SdkInt > 32) ? PackageManager.QueryIntentActivities(intent, resolveInfoFlags) :
            PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);

 and
  // the ResolveInfoFlags.Of(long value) will return a ResolveInfoFlags
  PackageManager.QueryIntentActivities(intent, Android.Content.PM.PackageManager.ResolveInfoFlags.Of(1234)) :
            PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);

Upvotes: 1

Kishan Mevada
Kishan Mevada

Reputation: 701

You need to change syntax :

1.

PackageManager.MatchDefaultOnly to PackageManager.MATCH_DEFAULT_ONLY

2.

PackageManager.ResolveInfoFlags to PackageManager.GET_RESOLVED_FILTER

Upvotes: 0

Related Questions