unity3d 獲取android id

unity3d 獲取android id
廢話不多直接上代碼

    public static string GetAndroidID()
    {
        string _strAndroidID = "none";
        if (string.IsNullOrEmpty(_strAndroidID))
        {
            _strAndroidID = "none";
#if (UNITY_ANDROID && !UNITY_EDITOR) || ANDROID_CODE_VIEW
            try
            {
                using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                {
                    using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                    {
                        using (AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject>("getContentResolver"))
                        {
                            using (AndroidJavaClass secure = new AndroidJavaClass("android.provider.Settings$Secure"))
                            {
                                _strAndroidID = secure.CallStatic<string>("getString", contentResolver, "android_id");
                                if (string.IsNullOrEmpty(_strAndroidID))
                                {
                                    _strAndroidID = "none";
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
            }
#endif
            return _strAndroidID;
        }

        return _strAndroidID;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章