【发布时间】:2021-09-11 02:25:18
【问题描述】:
我的应用适用于位置信息。所以我需要开启定位服务。我在iOS platform 上看到了有关检查位置服务状态的问题,但是有没有办法在共享项目中检查呢?
我正在寻找一种事件类型的解决方案,如果用户关闭位置服务,应用程序将停止它正在执行的操作并要求用户再次打开位置。
【问题讨论】:
标签: c# xamarin xamarin.forms service location
我的应用适用于位置信息。所以我需要开启定位服务。我在iOS platform 上看到了有关检查位置服务状态的问题,但是有没有办法在共享项目中检查呢?
我正在寻找一种事件类型的解决方案,如果用户关闭位置服务,应用程序将停止它正在执行的操作并要求用户再次打开位置。
【问题讨论】:
标签: c# xamarin xamarin.forms service location
您可以使用DependencyService查看位置是否开启。
这是我的cs代码:
private void Button_Clicked(object sender, EventArgs e)
{
var check = DependencyService.Get<IDeviceOrientationService>().IsLocationServiceEnabled();
if (check)
{
res.Text = "It is on";
}
else
{
res.Text = "It is off";
}
}
这是我的方法的实现代码(在 App.Android 中):
[assembly: Dependency(typeof(DeviceOrientationService))]
namespace App13.Droid
{
class DeviceOrientationService : IDeviceOrientationService
{
public bool IsLocationServiceEnabled()
{
LocationManager locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
return locationManager.IsProviderEnabled(LocationManager.GpsProvider);
}
}
}
截图如下:
【讨论】: