Xamarin C# for Android : Searching for an App and path

Issue

GOAL : I need to find if an app is installed on a device AND find it’s path..

I see using PackageManager, you can do this in general but I would like to refine it.

I know if you use com.google.chrome you can find chrome explorer installed, but this fails when you look simply for Chrome.

On some devices Chrome (and other apps, like Opera, Mini,etc) is not installed as com.google.chrome.

So how would one find an app without the com.google and just use Chrome as the search criteria ?

Solution

Simple, just combine PackageManager and ApplicationInfo, and then check if app name contain your search string. Here is some example code:

    var searchQuery  "chrome";
    var flag  PackageInfoFlags.Activities;
    var apps  PackageManager.GetInstalledApplications(flag);
    foreach(var app in apps)
    {
        try
        {
            var appInfo  PackageManager.GetApplicationInfo(app.PackageName, 0);
            var appLabel  PackageManager.GetApplicationLabel(appInfo);
            if (appLabel.ToLower().Contains(searchQuery.ToLower()))
            {
                var builder  new AlertDialog.Builder(this);
                builder.SetTitle("Found it!");
                builder.SetMessage(appLabel + " installed at: " + app.SourceDir);
                builder.Show();
            }
        }
        catch (PackageManager.NameNotFoundException e) { continue; }
    }

Answered By – I Putu Yoga Permana

Leave a Comment