嗚嗚喔學習筆記: 九月 2018

搜尋此網誌

2018年9月16日 星期日

Unity , Detect if app is installed on Android (確認是否有安裝這個APP)

Unity中確認是不是有安裝該APP
可以用PackageName 去查詢
至於怎麼拿PackageName 這裡就不多提了 0.0

上CODE:

    private static bool IsAndroidInstalledApp(string packageName)
    {
        try
        {
            AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject ca = up.GetStatic("currentActivity");
            AndroidJavaObject packageManager = ca.Call("getPackageManager");
            AndroidJavaObject appList = packageManager.Call("getInstalledPackages", 0);
            int num = appList.Call("size");
            for (int i = 0; i < num; i++)
            {
                AndroidJavaObject appInfo = appList.Call("get", i);
                string packageNew = appInfo.Get("packageName");
                if (packageNew.CompareTo(packageName) == 0)
                {
                    return true;
                }
            }
            return false;
        }
        catch (Exception e)
        {
            Debug.LogErrorFormat("[OpenUrlHelper][IsInstalledApp] Get Exception , PackageName {0}", packageName);
            return false;
        }
    }