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 = "com.package.name"){
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);
ca.Call("startActivity",launchIntent);
}
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.
Thanks!
D= I kept searching for solutions and came across my own posting as a result
– JChiltonDo you see an exception in logcat when you call this script for a package that is not installed ? From looking at the code, it seems like Unity attempts to catch the exception from JNI and rethrow that as an exception in Mono. Did you try surrounding the code that calls getLaunchIntentForPackage in a try...catch block (catch all exceptions) and print them out to the debug console.
– liortal