PackageManager - StansAssets/com.stansassets.android-native GitHub Wiki

The AN_PackageManager class an analog to Native Android PackageManager class and can be used for retrieving various kinds of information related to the application packages that are currently installed on the device.

First of all, you need to get AN_PackageManager instance. See the example below:

using SA.Android.App;
using SA.Android.Content.Pm;
...

AN_PackageManager pm = AN_MainActivity.Instance.GetPackageManager();

Then you can retrieve information about any installed application, including your

Package Info

Use the GetPackageInfo method when you need to retrieve overall information about any applications package that is installed on the system.

using SA.Android.App;
using SA.Android.Content.Pm;
...

string packageName = "any.app.package.name";
var pm = AN_MainActivity.Instance.GetPackageManager();
AN_PackageInfo packageInfo =  pm.GetPackageInfo(packageName, 0);
Debug.Log("packageInfo.VersionName: " + packageInfo.VersionName);
Debug.Log("packageInfo.PackageName: " + packageInfo.PackageName);
Debug.Log("packageInfo.SharedUserId: " + packageInfo.SharedUserId);

Query Intent Activities

You may use QueryIntentActivities, if you need to retrieve all activities that can be performed for the given intent. See the example below.

using SA.Android.App;
using SA.Android.Content;
using SA.Android.Content.Pm;
...

AN_Intent testIntent = new AN_Intent();
testIntent.SetAction(AN_Intent.ACTION_SEND);
testIntent.SetType("text/plain");

var pm = AN_MainActivity.Instance.GetPackageManager();
List<AN_ResolveInfo> resolveInfoList = pm.QueryIntentActivities(testIntent, 0);
foreach(var resolveInfo in resolveInfoList) {
    Debug.Log("resolveInfo.ActivityInfo.Name: " + resolveInfo.ActivityInfo.Name);
    Debug.Log("resolveInfo.ActivityInfo.PackageName: " + resolveInfo.ActivityInfo.PackageName);
}

Check If App Installed

The quick sample how you can check if a certain app is currently installed on the device. For example, let's check if facebook app is installed.

using SA.Android.App;
using SA.Android.Content;
using SA.Android.Content.Pm;
...

var pm = AN_MainActivity.Instance.GetPackageManager();
var info = pm.GetPackageInfo("com.facebook.katana", AN_PackageManager.GET_ACTIVITIES);
if(info != null) {
    Debug.Log("facebook installed");
}

Open External App

Use the GetLaunchIntentForPackage to get a "good" intent to launch a front-door activity in a package. This is used, for example, to implement an "open" button when browsing through packages. The current implementation looks first for a main activity in the category Intent.CATEGORY_INFO, and next for a main activity in the category Intent.CATEGORY_LAUNCHER. Returns null if neither are found. The example below describes how to launch the Facebook Android App.

using SA.Android.App;
using SA.Android.Content;
using SA.Android.Content.Pm;
...

var pm = AN_MainActivity.Instance.GetPackageManager();
AN_Intent startAppIntent = pm.GetLaunchIntentForPackage("com.facebook.katana");
if(startAppIntent == null) {
    AN_Logger.Log("App with Id: com.facebook.katana not found on device");
    return;
}
startAppIntent.AddCategory(AN_Intent.CATEGORY_LAUNCHER);

AN_MainActivity.Instance.StartActivity(startAppIntent);

If you need more API related to the native Android PackageManager, do not hesitate to contacts us directly. https://stansassets.com/