Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JChilton · Mar 26, 2013 at 07:50 PM · androidjava

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JChilton · Mar 28, 2013 at 08:02 PM 0
Share

D= I kept searching for solutions and came across my own posting as a result

avatar image liortal · May 26, 2014 at 06:11 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
         }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JChilton · Feb 12, 2016 at 12:47 AM 0
Share

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.

avatar image rushikesh988 · Feb 23, 2016 at 07:31 AM 1
Share

I know, I replied because it might help someone in community looking for the same solution.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JNI Exception in the editor [Android] Unity 4.2 4 Answers

AndroidJavaClass. Call returns null ptr. 1 Answer

Issue installing apk using old Cert 1 Answer

Accessing Data from a Android java class 1 Answer

How do I save a screenshot to the gallery on Android Kitkat 4.4.2 or above? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges