Why PackageManager GetInstalledApplications always triggering NotImplementedException?

102 Views Asked by At

I'm working on a C# Xamarin project where I need to get the list of installed apps from my android phone.

For this purpose I'm trying to use the method GetInstalledApplications from the PackageManager class.

Since the class is abstract, I can't use it directly. So I created a new class called PM2 that inherits all it's methods from it, including GetInstalledApplications.

Problem is when I try to use the method to get the List ApplicationInfo, it always triggers NotImplementedException, instead of returning the list.

Here's the code from the MyApp.Android\Services\HelperService.cs

        public IList<ApplicationInfo> PM()
    {
        IList<PackageInfo> lista = new List<PackageInfo>();
        PackageManager pm2 = new PM2();
        PackageInfoFlags flags = new PackageInfoFlags();
        IList<ApplicationInfo> packages = (IList<ApplicationInfo>)pm2.GetInstalledApplications(flags);
        return (IList<ApplicationInfo>)packages;
    }

This is the inheriting class with the inherited method. There are loads more methods but I just put in the relevant one:

[Obsolete]
public class PM2 : PackageManager
{
        public override IList<ApplicationInfo> GetInstalledApplications([GeneratedEnum] PackageInfoFlags flags)
        {
            throw new NotImplementedException();
        }
}

This is the interface through which I call the method outside the exclusively Android segment of the project. It's on MyApp\Services\IHelperService.cs:

public interface IHelperService {

    IList<ApplicationInfo> PM();
}

This is where I call the interface. It's on MyApp\Extensions\PaymentMethods.xaml.cs:

IList<ApplicationInfo> packages = DependencyService.Get<Services.IHelperService>().PM();

These are the permissions used on MyApp.Android\Properties\AssemblyInfo:

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
[assembly: UsesPermission(Android.Manifest.Permission.RequestInstallPackages)]
[assembly: UsesPermission(Android.Manifest.Permission.QueryAllPackages)]

I need to get the apps list because my app(A) calls a third party app(B) and if app B isn't installed we don't want it to just throw an exception. We want to design a customize alert telling the user what he needs to do, namely, download app B.

Any help would be very much appreciated since I need this for work and it's been more than a month that I'm stuck with this problem.

Thank you.

1

There are 1 best solutions below

1
Liyun Zhang - MSFT On BEST ANSWER

First of all, as Selvin said you can obtain instance of it from the Context if you want to use the PackageManager. Scuh as:

var packagemanager = Android.App.Application.Context.PackageManage;
or
var packagemanager = Android.App.Application.Context.ApplicationContext.PackageManager;

And then you said:

I need to get the apps list because my app(A) calls a third party app(B) and if app B isn't installed we don't want it to just throw an exception

You can refer to this thread: How to check an App is installed or not using MAUI App. Use the PackageManager.GetPackageInfo() method is also a good choice.