How to add a popup asking for permission access?
Xamarin
public void RequestPermissions()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
if (ContextCompat.CheckSelfPermission(MainActivity.AppInstance, Manifest.Permission.Camera) != Permission.Granted || ContextCompat.CheckSelfPermission(MainActivity.AppInstance, Manifest.Permission.RecordAudio) != Permission.Granted)
{
List<string> req = new List<string>();
if (ContextCompat.CheckSelfPermission(MainActivity.AppInstance, Manifest.Permission.Camera) != Permission.Granted)
{
req.Add(Manifest.Permission.Camera);
}
if (ContextCompat.CheckSelfPermission(MainActivity.AppInstance, Manifest.Permission.RecordAudio) != Permission.Granted)
{
req.Add(Manifest.Permission.RecordAudio);
}
ActivityCompat.RequestPermissions(MainActivity.AppInstance, req.ToArray(), PERMISSIONS_REQUEST_CAPTURE);
}
}
}
How to catch event when permission is turned on/off?
Xamarin
// MainActivity.cs
namespace VAST.Demo.Droid
{
...
private const int PERMISSIONS_REQUEST_CAPTURE = 1;
private const int PERMISSIONS_REQUEST_MEDIA_PROJECTION = 2;
public static MediaProjectionManager mediaProjectionManager = null;
public static Result mediaProjectionResultCode = 0;
public static Intent mediaProjectionResultData = null;
protected override void OnCreate(Bundle savedInstanceState)
{
...
}
protected override void OnResume()
{
base.OnResume();
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
if (CheckSelfPermission(Manifest.Permission.Camera) != Permission.Granted ||
CheckSelfPermission(Manifest.Permission.RecordAudio) != Permission.Granted)
{
List<string> req = new List<string>();
if (CheckSelfPermission(Manifest.Permission.Camera) != Permission.Granted)
{
req.Add(Manifest.Permission.Camera);
}
if (CheckSelfPermission(Manifest.Permission.RecordAudio) != Permission.Granted)
{
req.Add(Manifest.Permission.RecordAudio);
}
if (CheckSelfPermission(Manifest.Permission.ReadExternalStorage) != Permission.Granted)
{
req.Add(Manifest.Permission.ReadExternalStorage);
}
if (CheckSelfPermission(Manifest.Permission.WriteExternalStorage) != Permission.Granted)
{
req.Add(Manifest.Permission.WriteExternalStorage);
}
RequestPermissions(req.ToArray(), PERMISSIONS_REQUEST_CAPTURE);
}
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
mediaProjectionManager = (MediaProjectionManager)GetSystemService(Context.MediaProjectionService);
if (mediaProjectionResultCode != Result.Ok || mediaProjectionResultData == null)
{
StartActivityForResult(mediaProjectionManager.CreateScreenCaptureIntent(), PERMISSIONS_REQUEST_MEDIA_PROJECTION);
}
}
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == PERMISSIONS_REQUEST_MEDIA_PROJECTION)
{
if (resultCode != Result.Ok)
{
return;
}
mediaProjectionResultCode = resultCode;
mediaProjectionResultData = data;
// DO SOMETHING, START AN ANDROID SERVICE
...
}
base.OnActivityResult(requestCode, resultCode, data);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] global::Android.Content.PM.Permission[] grantResults)
{
switch (requestCode)
{
case PERMISSIONS_REQUEST_CAPTURE:
{
bool allGranted = true;
foreach (Permission res in grantResults)
{
if (res != Permission.Granted)
{
allGranted = false;
break;
}
}
if (allGranted)
{
}
break;
}
default:
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
}
}
How to navigate to permission setting screen?
Xamarin
public void CheckOverlayPermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
if (!Android.Provider.Settings.CanDrawOverlays(MainActivity.AppInstance))
{
Intent intent = new Intent(Android.Provider.Settings.ActionManageOverlayPermission);
intent.AddFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
}
}
}
Leave a Reply