- Home /
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 = "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
Do 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 $$anonymous$$ono. 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.
Answer by rushikesh988 · Feb 11, 2016 at 05:44 PM
@JChilton 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.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.example.rushiActivity"));
startActivity(intent);
}
Thanks for the response, but this question was asked almost 2 1/2 years ago. I can't remember why I wanted to know how to do this. Really amazes me though how much I've learned and grown since then.
I know, I replied because it might help someone in community looking for the same solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095139im_/https://answers.unity.com/themes/thub/images/avi.jpg)