У меня есть приложение, которому требуются разрешения для камеры, это хорошо реализовано, но запрашивает разрешение на главном экране (spla sh). То, что я хотел бы, чтобы появиться после входа в систему или на определенной странице c (это возможно?). И поскольку я мог бы реализовать то же самое для IOS, я очень благодарен вам за вашу помощь и хорошего дня.
Спасибо за вашу помощь.
Здесь мой код
MainActivity.cs
const int requestCameraId = 0;
const int requestStorageId = 1;
const int requestId = 2;
readonly string[] permissions =
{
Android.Manifest.Permission.Camera,
Android.Manifest.Permission.ReadExternalStorage,
Android.Manifest.Permission.WriteExternalStorage,
Android.Manifest.Permission.Internet,
Android.Manifest.Permission.ForegroundService,
Android.Manifest.Permission.RequestCompanionUseDataInBackground,
Android.Manifest.Permission.RequestCompanionRunInBackground,
Android.Manifest.Permission.StatusBar,
Android.Manifest.Permission.Vibrate,
Android.Manifest.Permission.Flashlight
};
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
switch (requestCode)
{
case requestCameraId:
{
if (grantResults[0] == (int)Android.Content.PM.Permission.Granted)
{
Toast.MakeText(this, "Permiso concedido para la camara", ToastLength.Short).Show();
}
else
{
//Permission Denied :(
Toast.MakeText(this, "Permiso denegado para la camara", ToastLength.Short).Show();
}
}
break;
case requestStorageId:
{
if (grantResults[0] == (int)Android.Content.PM.Permission.Granted)
{
Toast.MakeText(this, "Permiso concedido para el almacenamiento", ToastLength.Short).Show();
}
else
{
//Permission Denied :(
Toast.MakeText(this, "Permiso denegado para el almacenamiento", ToastLength.Short).Show();
}
}
break;
}
}
async Task GetCameraPermissionAsync()
{
const string permission = Manifest.Permission.Camera;
if (CheckSelfPermission(permission) == (int)Android.Content.PM.Permission.Granted)
{
//TODO change the message to show the permissions name
Toast.MakeText(this, "Permisos para la camara listos", ToastLength.Short).Show();
return;
}
if (ShouldShowRequestPermissionRationale(permission))
{
//set alert for executing the task
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Permisos necesarios");
alert.SetMessage("La aplicación necesita acceder a la camara para tomar una fotografía del trabajo terminado");
alert.SetPositiveButton("Conceder permiso", (senderAlert, args) =>
{
RequestPermissions(permissions, requestCameraId);
});
alert.SetNegativeButton("Cancelar", (senderAlert, args) =>
{
Toast.MakeText(this, "Cancelado", ToastLength.Short).Show();
});
Dialog dialog = alert.Create();
dialog.Show();
return;
}
}
async Task GetStoragePermissionAsync()
{
const string permission = Manifest.Permission.ReadExternalStorage;
if (CheckSelfPermission(permission) == (int)Android.Content.PM.Permission.Granted)
{
//TODO change the message to show the permissions name
Toast.MakeText(this, "Permisos para leer carpetas listos", ToastLength.Short).Show();
return;
}
if (ShouldShowRequestPermissionRationale(permission))
{
//set alert for executing the task
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Permisos necesarios");
alert.SetMessage("La aplicación necesita acceder a sus archivos para subir una imagen con el trabajo terminado");
alert.SetPositiveButton("Conceder permiso", (senderAlert, args) =>
{
RequestPermissions(permissions, requestStorageId);
});
alert.SetNegativeButton("Cancelar", (senderAlert, args) =>
{
Toast.MakeText(this, "Cancelado", ToastLength.Short).Show();
});
Dialog dialog = alert.Create();
dialog.Show();
return;
}
}
async Task GetPermissionsAsync()
{
await GetCameraPermissionAsync();
await GetStoragePermissionAsync();
RequestPermissions(permissions, requestId);
}
async Task TryToGetPermissions()
{
if ((int)Build.VERSION.SdkInt >= 23)
{
await GetPermissionsAsync();
return;
}
}
protected async override void OnCreate(Bundle savedInstanceState)
{
await TryToGetPermissions();
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// RequestPermissions(permissions, requestId);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
CreateNotificationFromIntent(Intent);
//notificationServiceIntent = new Intent(this.BaseContext, typeof(PDANotificationService));
//StartService(notificationServiceIntent);
WireUpLongRunningTask();
var message = new StartLongRunningTaskMessage();
MessagingCenter.Send(message, "StartLongRunningTaskMessage");
}