如何在用户单击时再次调用权限对话框不再询问
How to call permission dialog again when user clicked don't ask again
大家好,我制作了一个 xamarin.forms 应用程序。我需要请求用户允许应用使用他的位置。这是我的代码:
var location = await Geolocation.GetLastKnownLocationAsync();
if (location == null)
{
location = await Geolocation.GetLocationAsync(new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.Medium,
Timeout = TimeSpan.FromSeconds(30)
}); ;
}
if (location == null)
LabelLocation.Text = " NO GPS LOCATION";
else
LabelLocation.Text = $"{location.Latitude} {location.Longitude}";
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
catch (PermissionException pEx)
{
// Handle permission exception
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
catch (Exception ex)
{
// Unable to get location
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
当用户点击不再显示时,每次打开页面都会继续 PermissionException
没关系。但我想再次显示权限对话框,因为没有位置就无法显示数据。可以这样做吗?
再次简短的回答是,不,你不能。正如您在请求权限 documentation 中看到的那样(当用户拒绝权限并检查从这一刻起不应显示任何提示时):
Explain to the user that the feature is unavailable because the features requires a permission that the user has denied. At the same time, respect the user's decision. Don't link to system settings in an effort to convince the user to change their decision.
最佳做法是在您请求许可之前向用户详细解释。如果用户仍然决定拒绝您此权限,则显示一条消息,说明此特定逻辑根本行不通。这是 反对 的最佳做法,但如果您愿意,可以将用户重定向到 Settings
,用户可以在其中启用权限。但是,我强烈建议您不要这样做。
一个重要的旁注,同样来自 documentation:
If the ContextCompat.checkSelfPermission()
method returns PERMISSION_DENIED
, call shouldShowRequestPermissionRationale()
. If this method returns true
, show an educational UI to the user. In this UI, describe why the feature, which the user wants to enable, needs a particular permission.
另外,permission checking 使用 Xamarin.Essentials
。那里的一切都有很好的记录。
P.S。我强烈建议您阅读 App permissions best practices,在那里您可以找到来自官方 Android 团队的其他有用实践。
大家好,我制作了一个 xamarin.forms 应用程序。我需要请求用户允许应用使用他的位置。这是我的代码:
var location = await Geolocation.GetLastKnownLocationAsync();
if (location == null)
{
location = await Geolocation.GetLocationAsync(new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.Medium,
Timeout = TimeSpan.FromSeconds(30)
}); ;
}
if (location == null)
LabelLocation.Text = " NO GPS LOCATION";
else
LabelLocation.Text = $"{location.Latitude} {location.Longitude}";
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
catch (PermissionException pEx)
{
// Handle permission exception
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
catch (Exception ex)
{
// Unable to get location
await Application.Current.MainPage.Navigation.PushAsync(new MainMenu());
}
当用户点击不再显示时,每次打开页面都会继续 PermissionException
没关系。但我想再次显示权限对话框,因为没有位置就无法显示数据。可以这样做吗?
再次简短的回答是,不,你不能。正如您在请求权限 documentation 中看到的那样(当用户拒绝权限并检查从这一刻起不应显示任何提示时):
Explain to the user that the feature is unavailable because the features requires a permission that the user has denied. At the same time, respect the user's decision. Don't link to system settings in an effort to convince the user to change their decision.
最佳做法是在您请求许可之前向用户详细解释。如果用户仍然决定拒绝您此权限,则显示一条消息,说明此特定逻辑根本行不通。这是 反对 的最佳做法,但如果您愿意,可以将用户重定向到 Settings
,用户可以在其中启用权限。但是,我强烈建议您不要这样做。
一个重要的旁注,同样来自 documentation:
If the
ContextCompat.checkSelfPermission()
method returnsPERMISSION_DENIED
, callshouldShowRequestPermissionRationale()
. If this method returnstrue
, show an educational UI to the user. In this UI, describe why the feature, which the user wants to enable, needs a particular permission.
另外,permission checking 使用 Xamarin.Essentials
。那里的一切都有很好的记录。
P.S。我强烈建议您阅读 App permissions best practices,在那里您可以找到来自官方 Android 团队的其他有用实践。