Issue
My app works with location. So I it is required to be location service on. I have seen questions about checking location service's status on the iOS platform, but is there a way to check this in the shared project?
I'm looking for an event type solution where if the user turns off the location service, the app will stop what it was doing and ask the user to turn on the location again.
Solution
You can use DependencyService to see if the location is turned on.
Here is my cs code:
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";
}
}
Here is the code for the implementation of my method (in 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);
}
}
}
Here is the screenshot:
Answered By - Wen xu Li - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.