【问题标题】:How to check if the location service is "on" in Xamarin Forms?如何检查 Xamarin Forms 中的位置服务是否“开启”?
【发布时间】:2021-09-11 02:25:18
【问题描述】:

我的应用适用于位置信息。所以我需要开启定位服务。我在iOS platform 上看到了有关检查位置服务状态的问题,但是有没有办法在共享项目中检查呢?

我正在寻找一种事件类型的解决方案,如果用户关闭位置服务,应用程序将停止它正在执行的操作并要求用户再次打开位置。

【问题讨论】:

    标签: c# xamarin xamarin.forms service location


    【解决方案1】:

    您可以使用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);
            }
        }
    }
    

    截图如下:

    【讨论】:

    • 所有的人都在投下选票,让我在你给我希望的地方失去动力。
    • 祝你成功
    • 我也遇到了同样的问题。
    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2014-08-22
    • 1970-01-01
    • 2021-12-28
    • 2016-04-24
    相关资源
    最近更新 更多