Using PackageManager to launch/check app on Android

Hi there, I’m trying to use a series of AndroidJavaObjects to locate the PackageManager, and then use it to launch other apps.

It works as long as the other app is already installed, but I’m having trouble checking for if it is not installed yet.

My main qualm is understanding how to use/handle Exceptions that are called in Java, while inside my own script.

Here’s what I have so far:

	private void LaunchOtherApp(string bundleID = ""){
		AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		AndroidJavaClass ca = up.GetStatic<AndroidJavaObject>("currentActivity");
		AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
		//if the app is installed, no errors. Else, doesn't get past next line
		AndroidJavaObject launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage",bundleID);


Using getLaunchIntentForPackage, or getPackageInfo I know both throw a PackageManager.NameNotFoundException if the app isn’t installed,
but I can’t seem to figure out how to catch that as it’s a Java exception, and not unity.

Any ideas?
If the app is installed, it works just fine.
I just have an issue trying to catch that Java exception, or check if my intent is null.

The app crashes out completely on that line.


I know this might not be the exact way you are looking but may be you can try that in java. Create a plugin for the same,

	Intent intent = this.getPackageManager().getLaunchIntentForPackage("com.example.rushiActivity");
		if (!(intent != null)) {
			// Bring user to the market or let them choose an app?
			intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse("market://details?id=" + "com.example.rushiActivity"));