问题描述
每当我打开我的应用程序时,我都需要检查位置是打开还是关闭.如果位置关闭,我需要向用户显示提醒以启用位置共享,如下面的屏幕截图:
Whenever I am opening my app I need to check the location is on or off. If the location is off, I need to show an alert to the user to enable location share like the below screenshot:
我尝试使用此的依赖项服务线程:
共享项目的界面:
public interface ILocSettings
{
void OpenSettings();
}
Android 实现
[assembly: Dependency(typeof(LocationShare))]
namespace ProjectName.Droid
{
public class LocationShare : ILocSettings
{
public void OpenSettings()
{
//code1
Android.App.Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings));
//code2
//LocationManager LM = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
//if (LM.IsProviderEnabled(LocationManager.GpsProvider) == false)
//{
// Context ctx = Android.App.Application.Context;
// ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings));
//}
}
}
}
最后来自如下调用的共享项目:
var myAction = await DisplayAlert("Location", "Please Turn On Location", "OK", "CANCEL");
if (myAction)
{
if (Device.RuntimePlatform == global::Xamarin.Forms.Device.Android)
{
DependencyService.Get<ILocSettings>().OpenSettings();
}
}
else
{
await DisplayAlert("Alert", "User Denied Permission", "OK");
}
我在运行这个时遇到异常.(code1 和 code2 得到相同的异常)
I am getting below exception when running this. (Getting the same exception for code1 and code2)
System.NullReferenceException: '未将对象引用设置为对象的实例.'
我只需要在位置关闭时显示警报.如果位置打开,则无需执行这些操作.我如何检查位置是打开还是关闭?
I need to show the alert only if the location is off. If the location is on, no need to do these things. How I can check the location is on or off?
另外,我需要为 ios 和 windows 平台实现相同的功能.
嗨@Lucas Zhang - MSFT
Hi @Lucas Zhang - MSFT
我已经尝试了您的解决方案并收到了类似这个的提醒.但是在授予位置访问权限后,设备的 location 仍然是离开.我需要在设备的位置像 this 当用户点击警报中的确定选项(问题截图).直接在位置上或重定向到位置设置页面.
I have tried your solution and got an alert like this. But after giving the location access, still the device's location is off. I need to on the device's location like this when the user taps the OK option in the alert (question screenshot). Either on the location directly or redirect to the location settings page.
尝试了 GeolocatorPlugin 并使用以下代码来检查 GPS 是关闭还是打开.即使 GPS 开启,也总是得到 False 值.
Tried GeolocatorPlugin and used the below code for checking the GPS is off or on. Always getting False value even if the GPS is on.
public bool IsLocationAvailable()
{
if (!CrossGeolocator.IsSupported)
return false;
return CrossGeolocator.Current.IsGeolocationAvailable;
}
对android服务做了以下修改,现在我可以打开位置设置了.
Made below modification on the android service and now I am able to open the location settings.
public class LocationShare : ILocSettings
{
public void OpenSettings()
{
Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
在打开位置设置页面之前,我需要验证 GPS 是否为 on 或 off(不是位置许可).
Before opening the location settings page, I need to verify the GPS is on or off (not the location permission).
我也不明白杰克的 ios 回答.那么你能像我为android打开ios位置设置页面那样向我展示ios依赖服务吗?
Also I didn't understand the ios answer by Jack. So can you show me the ios dependency service like I did for android for opening ios location settings page?
推荐答案
在你的情况下,你可以使用插件 PermissionsPlugin 来自 Nuget.
In your case you could use the plugin PermissionsPlugin from Nuget.
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
}
if (status == PermissionStatus.Granted)
{
//Query permission
}
else if (status != PermissionStatus.Unknown)
{
//location denied
}
}
catch (Exception ex)
{
//Something went wrong
}
更新
你好像想检查系统位置是否打开,对吧?如果是这样,您可以在获得位置权限后尝试获取 GPS 信息.如果 GPS 信息仍然不可用,则表示系统设置为 OFF.您可以调用依赖服务打开平台设置页面.
Update
It seems that you want to check if system location is open or not , right ? If so , you could try to achieve GPS info after you get the location permission . If the GPS info is still unavailable , that means the system setting is OFF .And you can invoke dependency service to open platform setting page.
public async void ShareLocation()
{
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
if (status == PermissionStatus.Granted)
{
bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
if (!gpsStatus)
{
var myAction = await DisplayAlert("Location", "Please turn on GPS for the proper working of the application.", "TURN ON", "CANCEL");
if (myAction)
{
DependencyService.Get<ILocSettings>().OpenSettings();
}
}
}
}
//ILocSettings
public interface ILocSettings
{
void OpenSettings();
bool isGpsAvailable();
}
//Android Dependency Service
[assembly: Dependency(typeof(LocationShare))]
namespace Projectname.Droid.Services
{
public class LocationShare : ILocSettings
{
public bool isGpsAvailable()
{
bool value = false;
Android.Locations.LocationManager manager = (Android.Locations.LocationManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService);
if (!manager.IsProviderEnabled(Android.Locations.LocationManager.GpsProvider))
{
//gps disable
value = false;
}
else
{
//Gps enable
value = true;
}
return value;
}
public void OpenSettings()
{
Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
}
iOS 版
public void CheckAuthorization(CLLocationManager manager, CLAuthorizationStatus status)
{
switch (status)
{
case CLAuthorizationStatus.Authorized | CLAuthorizationStatus.AuthorizedAlways | CLAuthorizationStatus.AuthorizedWhenInUse:
Console.WriteLine("Access");
break;
case CLAuthorizationStatus.Denied | CLAuthorizationStatus.NotDetermined | CLAuthorizationStatus.Restricted:
Console.WriteLine("No Access");
break;
default:
Console.WriteLine("No Access");
break;
}
}
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
这篇关于Xamarin Forms:如何实现位置共享功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!