Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[SOLVED] Check if an app is installed on my phone

Discussion in 'Android' started by Vasilis62, Jul 9, 2019.

  1. Vasilis62

    Vasilis62

    Joined:
    Nov 2, 2018
    Posts:
    15
    Hey all,

    So I have created a function that checks if an app is installed on my phone. If it is then it opens the app, if it is not then i opens the google play store. Problem is whenever I try to run it, I get NullReferenceException: Object reference not set to an instance of an object on the AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");

    Any ideas what may be causing that?

    Code (CSharp):
    1. public void Installed()
    2.     {
    3. #if UNITY_ANDROID
    4.         string bundleId = "myBundleId";
    5.  
    6.         AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    7.         AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
    8.         AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
    9.         AndroidJavaObject launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage",bundleId);
    10.         if (launchIntent == null)
    11.         {
    12.             Application.OpenURL("market://details?id=" + bundleId);
    13.         }
    14.         else
    15.         {
    16.             ca.Call("startActivity",launchIntent);
    17.         }
    18.  
    19.         up.Dispose();
    20.         ca.Dispose();
    21.         packageManager.Dispose();
    22.         launchIntent.Dispose();
    23. #endif
    24.     }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    8,153
  3. Vasilis62

    Vasilis62

    Joined:
    Nov 2, 2018
    Posts:
    15
    Good day Jeff and thank you for your reply. Google searches are always useful, that is why I did multiple before asking here. However, in the link you posted a different problem is asked and solved. Specifically on my code I get a null reference exception on line 8 (the package manager) which after some debugging shows that the AndroidJavaObject ca is null, which means the GetStatic of currentActivity is null. That is what I am trying to understand and find out why is happening.
     
  4. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    448
    Sorry but I have to ask since you never mentioned it, but did you run it on android phone or on editor?
    [EDIT]
    nevermind, there would be no error code shouldnt be running if it run from editor since there's a #IF UNITY_ANDROID

    [Another EDIT]
    I'm having the same problem a while ago, (most likely following the same source reference as yours), but I forgot how I managed to solve it
    Maybe this might help
    https://stackoverflow.com/questions...ivity-in-unity-and-i-am-getting-the-following

    basically adding
    Code (csharp):
    1.  
    2. <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    3.  
     
    Last edited: Jul 10, 2019
  5. Vasilis62

    Vasilis62

    Joined:
    Nov 2, 2018
    Posts:
    15
    Hello Bart!

    I have tried that solution before and I had some conflicts with Firebase that I have integrated in my app. I will try what you are proposing and get back to you. Thank you!
     
  6. Vasilis62

    Vasilis62

    Joined:
    Nov 2, 2018
    Posts:
    15
    Well, I'll be damned! Even though my androidmanifest file had that line inside the activity block, adding it outside of it worked! Thank you so much Bart!
     
  7. aj-fix

    aj-fix

    Joined:
    Sep 4, 2018
    Posts:
    6
    Big heads up to anyone looking to use this technique now: Android 11 introduced limitations on the packages your app can see. So even if someone has the app installed, it won't appear to be installed unless you explicitly declare that you check for it in your Android manifest.

    This page details how you make the declaration:
    https://developer.android.com/training/basics/intents/package-visibility

    And if you're using 2019.4 or earlier, you'll have to modify your build as outlined below:
    https://developers.google.com/ar/develop/unity/android-11-build

    Hopefully this saves someone the hours it took me to find o_O
     
unityunity