(Solved) Unable to check app installation on Android

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).

    public bool CheckAppInstallation (string bundleId)
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject packageManager = curActivity.Call<AndroidJavaObject>("getPackageManager");

        AndroidJavaObject launchIntent = null;
        try
        {
            launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
            return true;
        }

        catch (System.Exception e)
        {
            return false;
        }
    }

Anyone could come up with anything that I could be doing wrong?

Thanks in advance!

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

    public bool CheckAppInstallation (string bundleId)
    {
        bool installed = false;
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject packageManager = curActivity.Call<AndroidJavaObject>("getPackageManager");

        AndroidJavaObject launchIntent = null;
        try
        {
            launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
            if (launchIntent == null)
                installed = false;

            else
                installed = true;
        }

        catch (System.Exception e)
        {
            installed = false;
        }
        return installed;
    }
6 Likes