如何在Xamarin Forms中检查位置服务是否已打开?

5

我的应用程序需要使用位置信息,因此需要开启位置服务。我看到了一些有关在iOS平台检查位置服务状态的问题,但是否有一种方法可以在共享项目中检查这一点?

我正在寻找一种事件类型的解决方案,如果用户关闭位置服务,则应用程序将停止当前操作并要求用户重新开启位置服务。

2个回答

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

这是截图:

enter image description here


2
所有人都在给我负面评价,让我感到失落,而你却给了我希望。 - Dhu al-Qarnayn
2
祝你成功 - Wen xu Li
1
我也遇到了同样的问题。 - Mahmudul Hasan

2

您可以使用DependancyService来查看位置是否已打开。有些人阅读此问题可能会想知道如何在iOS上执行此操作,因此我将为您介绍。

首先,我们应该在共享代码的Service文件夹中创建一个接口,其中包含:

public interface IGpsDependencyService
{ 
        bool IsGpsTurnedOn();
        void OpenSettings();
}

这里有两个函数:

  • IsGpsTurnedOn():用于检查GPS是否开启
  • OpenSettings():用于打开位置设置页面,让用户开启GPS功能。

现在我们需要编写这两个函数背后的本地代码:

在Service文件夹中为我们的iOS和Android项目创建GpsDependancyService.cs文件

在iOS中,我们可以编写如下代码:

[assembly: Xamarin.Forms.Dependency(typeof(GpsDependencyService))]

namespace YourApp.iOS.Services
{
    public class GpsDependencyService : IGpsDependencyService
    {

        public bool IsGpsTurnedOn()
        {
            if (CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void OpenSettings()
        {
            var WiFiURL = new NSUrl("prefs:root=WIFI");

            if (UIApplication.SharedApplication.CanOpenUrl(WiFiURL))
            {   //> Pre iOS 10
                UIApplication.SharedApplication.OpenUrl(WiFiURL);
            }
            else
            {   //> iOS 10
                UIApplication.SharedApplication.OpenUrl(new NSUrl("App-Prefs:root=WIFI"));
            }
        }
    }
}

然后,对于Android,我们可以编写:

[assembly: Xamarin.Forms.Dependency(typeof(GpsDependencyService))]
namespace YourApp.Droid.Services
{

    public class GpsDependencyService : IGpsDependencyService
    {
        public bool IsGpsTurnedOn()
        {
            LocationManager locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
            return locationManager.IsProviderEnabled(LocationManager.GpsProvider);
        }

        public void OpenSettings()
        {
            Intent intent = new Intent(Android.Provider.Settings.ActionLocat‌​ionSourceSettings);
            intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);

            try
            {
                Android.App.Application.Context.StartActivity(intent);

            }
            catch (ActivityNotFoundException activityNotFoundException)
            {
                System.Diagnostics.Debug.WriteLine(activityNotFoundException.Message);
                Android.Widget.Toast.MakeText(Android.App.Application.Context, "Error: Gps Activity", Android.Widget.ToastLength.Short).Show();
            }
        }
    }
}

现在,我们只需要在我们可爱的代码中调用这两个函数!

  • 使用IsGpsTurnedOn():DependencyService.Get().IsGpsTurnedOn();
  • 使用OpenSettings():DependencyService.Get().OpenSettings();

希望这能有所帮助,如果您有任何问题,请在评论区提问。


来源:https://github.com/xamarin/Essentials/issues/1257#issuecomment-623029612


为什么这要变得复杂呢? 为什么不使用内置的抽象化、非特定平台的Microsoft.Maui.ApplicationModel.Permissions类呢? 嗯,这就是我所做的,我在这里错过了什么吗? - undefined
@axa 如果你有更好的解决方案,为什么不自己提出答案呢? - undefined
嗯,正如评论中使用的两个问号所暗示的那样,这更像是一个问题而不是一个答案。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接