目录
1.先上最终研究成果(unity已经提供了面向Android的 权限申请方法)
2.疑难问题
3.原理解析:
1.先上最终研究成果(unity已经提供了面向Android的 权限申请方法)
ex:UnityEngine.Android.Permission.RequestUserPermission(s);
重要提示:使用unity的动态权限申请可以动态的弹出是否同意该权限界面,方便了很多
string[] strs=new string[] {
"android.permission.INTERNET",
"android.permission.READ_PHONE_STATE",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.ACCESS_WIFI_STATE",
"android.permission.ACCESS_NETWORK_STATE",
"android.permission.GET_TASKS",
"android.permission.REQUEST_INSTALL_PACKAGES",
"android.permission.WAKE_LOCK",
"android.permission.SYSTEM_ALERT_WINDOW",
"android.permission.CHANGE_WIFI_STATE",
"android.permission.CHANGE_NETWORK_STATE",
"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.SYSTEM_OVERLAY_WINDOW",
"android.permission.ACCESS_COARSE_UPDATES",
"android.permission.WRITE_SETTINGS",
"android.permission.BATTERY_STATS",
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
};
// Use this for initialization
void Start () {
Debug.Log("start main");
// var iscon= PlayerConnection.instance.isConnected;
strs.ToList().ForEach(s=>{
//Debug.Log("RequestUserPermission: "+s);
//if (!Permission.HasUserAuthorizedPermission(s))
//{
Permission.RequestUserPermission(s);
Debug.Log("add RequestUserPermission: " + s);
//}
//else
//{
// Debug.Log("it has RequestUserPermission: " + s);
//}
});
//UnityEngine.Networking.PlayerConnection.PlayerConnection.
//实例化上下文环境
// adUnionContext = AdUnionContext.GetInstance();
////初始化SDK
//AdUnionSDK.Init(APP_ID, new OnAuInitListenerProxy());
////初始化unity自带的控件
initComponents();
}
2.疑难问题
中间遇到的问题,配置在AndroidManifest.xml中的权限有时候会导致不能动态申请到所需权限,所以可以全部注释掉,然后动态添加。
3.原理解析:
其实调用的是Android方法
com.example.androidpermissionmgr.PermissionProvider 中的verifyPermissions方法
#if UNITY_ANDROID
private AndroidJavaClass _request;
public AndroidJavaClass Request {
get {
if (_request == null)
_request = new AndroidJavaClass ("com.example.androidpermissionmgr.PermissionProvider");
return _request;
}
}
#else
public void RequestPermissions (int requestCode, params string[]permissions)
{
Debug.Log ("[PermissionProvider] Requesting permissions: "+ requestCode + " " + permissions );
Request.CallStatic ("verifyPermissions", gameObject.name, requestCode, permissions);
}