|
@@ -1,25 +1,53 @@
|
|
|
|
|
+using System;
|
|
|
using System.Collections;
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
using UnityEngine;
|
|
|
|
|
+using UnityEngine.Android;
|
|
|
|
|
|
|
|
public class GPSTool
|
|
public class GPSTool
|
|
|
{
|
|
{
|
|
|
//GPS获取地址
|
|
//GPS获取地址
|
|
|
- //成功返回:string[]{国,省,市}
|
|
|
|
|
|
|
+ //成功返回:string[]{国,省,市} (数组中不允许有null)
|
|
|
//失败返回:null
|
|
//失败返回:null
|
|
|
- public static string[] GetAddress()
|
|
|
|
|
- {
|
|
|
|
|
- string[] address = null;
|
|
|
|
|
- using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static void GetAddress(Action<string[]> callback) {
|
|
|
|
|
+ #if UNITY_ANDROID
|
|
|
|
|
+ Func<string[]> func = delegate() {
|
|
|
|
|
+ using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
|
using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
|
|
using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
|
|
|
|
|
+ using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
|
|
|
{
|
|
{
|
|
|
- using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
|
|
|
|
|
- {
|
|
|
|
|
- address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
|
|
|
|
|
|
|
+ string[] address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
|
|
|
|
|
+ if (address != null) {
|
|
|
|
|
+ foreach (var item in address)
|
|
|
|
|
+ if (item == null) return null;
|
|
|
}
|
|
}
|
|
|
|
|
+ return address;
|
|
|
}
|
|
}
|
|
|
|
|
+ };
|
|
|
|
|
+ List<string> permissions = new List<string>();
|
|
|
|
|
+ permissions.Add(Permission.CoarseLocation);
|
|
|
|
|
+ permissions.Add(Permission.FineLocation);
|
|
|
|
|
+ for (int i = permissions.Count - 1; i >= 0; i--) {
|
|
|
|
|
+ if (Permission.HasUserAuthorizedPermission(permissions[i])) permissions.RemoveAt(i);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (permissions.Count > 0) {
|
|
|
|
|
+ var requestCB = new PermissionCallbacks();
|
|
|
|
|
+ int needPermissionCount = permissions.Count;
|
|
|
|
|
+ requestCB.PermissionGranted += (s) => {
|
|
|
|
|
+ Debug.Log("用户同意" + s);
|
|
|
|
|
+ needPermissionCount--;
|
|
|
|
|
+ if (needPermissionCount == 0) callback.Invoke(func.Invoke());
|
|
|
|
|
+ };
|
|
|
|
|
+ requestCB.PermissionDenied += (s) => {
|
|
|
|
|
+ Debug.Log("用户拒绝" + s);
|
|
|
|
|
+ };
|
|
|
|
|
+ requestCB.PermissionDeniedAndDontAskAgain += (s) => {
|
|
|
|
|
+ Debug.Log("用户拒绝且要求不再询问" + s);
|
|
|
|
|
+ };
|
|
|
|
|
+ Permission.RequestUserPermissions(permissions.ToArray(), requestCB);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ callback?.Invoke(func.Invoke());
|
|
|
}
|
|
}
|
|
|
- return address;
|
|
|
|
|
|
|
+ #endif
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|