Search Unity

(Solved) Unable to check app installation on Android

Discussion in 'Android' started by Skibis, Sep 22, 2017.

  1. Skibis

    Skibis

    Joined:
    Mar 9, 2015
    Posts:
    2
    Hello!

    We are making android app that needs to check if theres currently other apps with given bundle id installed.
    Been using this bit of code succesfully before but recently it has started to claim every app is installed (and theyre not).

    Code (CSharp):
    1.     public bool CheckAppInstallation (string bundleId)
    2.     {
    3.         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    4.         AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    5.         AndroidJavaObject packageManager = curActivity.Call<AndroidJavaObject>("getPackageManager");
    6.  
    7.         AndroidJavaObject launchIntent = null;
    8.         try
    9.         {
    10.             launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
    11.             return true;
    12.         }
    13.  
    14.         catch (System.Exception e)
    15.         {
    16.             return false;
    17.         }
    18.     }
    Anyone could come up with anything that I could be doing wrong?

    Thanks in advance!
     
    Last edited: Sep 28, 2017
  2. Skibis

    Skibis

    Joined:
    Mar 9, 2015
    Posts:
    2
    Okay managed to find the fix on this problem, apparently the 'catch' bit was no longer catching any exceptions for some reason.

    New code now checks if the launchIntent is still null after 'try' as follows
    Code (CSharp):
    1.     public bool CheckAppInstallation (string bundleId)
    2.     {
    3.         bool installed = false;
    4.         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    5.         AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    6.         AndroidJavaObject packageManager = curActivity.Call<AndroidJavaObject>("getPackageManager");
    7.  
    8.         AndroidJavaObject launchIntent = null;
    9.         try
    10.         {
    11.             launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
    12.             if (launchIntent == null)
    13.                 installed = false;
    14.  
    15.             else
    16.                 installed = true;
    17.         }
    18.  
    19.         catch (System.Exception e)
    20.         {
    21.             installed = false;
    22.         }
    23.         return installed;
    24.     }