Xamarin 动态申请 android 权限

if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
            {
                var permission = Permission.ReadExternalStorage;
                if (ContextCompat.CheckSelfPermission(this, permission) != Android.Content.PM.Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new String[] { permission }, 1);
                }
            }

 一次申请多个授权

if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
            {
                var permission = new string[] { Permission.ReadExternalStorage, Permission.WriteExternalStorage, Permission.Camera };
                var requestPermissions = permission.Where(p => ContextCompat.CheckSelfPermission(this, p) != Android.Content.PM.Permission.Granted).ToList();
                if (requestPermissions.Count >0 )
                    ActivityCompat.RequestPermissions(this, requestPermissions.ToArray(), 1);

                var unauthorizedPermissions = permission.Where(p => ContextCompat.CheckSelfPermission(this, p) != Android.Content.PM.Permission.Granted).ToList();
                if (unauthorizedPermissions.Count > 0)
                {
                    Toast.MakeText(this, "部分授权未被允许", ToastLength.Long).Show();
                    return;
                }
            }

 

上一篇:DevExpress Xamarin.Forms v21.1 - 让移动应用界面更酷炫!


下一篇:Xamarin相关的socket网络编程案例以及坑