Bug 56667 - locationManager.IsProviderEnabled(...) does not work correctly
Summary: locationManager.IsProviderEnabled(...) does not work correctly
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings ()
Version: 7.3 (15.2)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2017-05-19 12:31 UTC by diegoxleon
Modified: 2018-04-09 11:06 UTC (History)
3 users (show)

Tags: bb
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description diegoxleon 2017-05-19 12:31:51 UTC
Given the following code:

using System;

using Android.App;
using Android.Content;
using Android.Locations;
using Android.Util;

[BroadcastReceiver(Exported = false)]
    [IntentFilter(new[] { "android.location.PROVIDERS_CHANGED" })]
    public class ProviderChangeReceiver : BroadcastReceiver, IEnableLogger
    {
        public override void OnReceive(Context context, Intent intent)
        {
            try
            {
                var locationManager = (LocationManager)context.GetSystemService(Context.LocationService);
                var gpsEnabled = locationManager.IsProviderEnabled(LocationManager.GpsProvider);
                var networkEnabled = locationManager.IsProviderEnabled(LocationManager.NetworkProvider);

                this.Log().Info($"\nGPS enabled: {gpsEnabled}\nNetwork enabled: {networkEnabled}");

            }
            catch (Exception e)
            {
                Log.Error(nameof(GeofenceBootReceiver), e.Message);
            }
        }
    }

gpsEnabled will be always true, even disabling gps or network.

However, networkEnabled will match the current "gps" state. I mean:
Go to device settings and disable Location services => networkEnabled == false
Go to device settings and enlable Location services => networkEnabled == true

So the problem seams that locationManager.IsProviderEnabled(LocationManager.NetworkProvider) is reacting to gps changes only (not network related) and locationManager.IsProviderEnabled(LocationManager.GpsProvider) is doing nothing
Comment 1 diegoxleon 2017-05-19 13:59:15 UTC
I created a POC with that may help: https://github.com/xleon/geofencing-playground
Comment 2 Jon Douglas [MSFT] 2017-06-23 18:12:46 UTC
I've tested this behavior and I cannot replicate your observations. I was able to see "gpsEnabled" to be false when turning off location, and see "gpsEnabled" to be true when turning location on. This seems to be expected behavior. I believe this can also depend on the device you are testing with as the APIs have some unexpected results(You can see many questions on Stack Overflow about these providers and certain things not working). If you find a sample in Android Studio that differs from Xamarin.Android, than we can consider it a bug. However I believe these APIs are working as intended and just have different behavior on various devices, networks, gps, signal strength, etc. I am marking this as RESOLVED ANSWERED for the time being. If you do find behavior to differ between Android Studio and Xamarin.Android using the same code, feel free to reopen this bug.

This was tested on a Nexus 5 using:

Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.13) Release
VisualStudio.15.Release/15.2.0+26430.13
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Xamarin   4.5.0.486 (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.3.1.2 (9dbc4c5)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.10.0.37 (ad35de4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 3 diegoxleon 2017-06-23 19:56:38 UTC
So you resolve to close a bug because it works "on my device"?
My test device is a Nexus 6 with the same setup you have (26430.13) and it doesn´t work. I´m not using Android Studio so I can´t compare both results.
Comment 4 Jon Douglas [MSFT] 2017-06-23 21:04:12 UTC
The fact of this matter is that there are many different behaviors with these APIs in Android that depend on an exact setup to replicate. This is due to Android's fragmentation.

I have resolved answered this question to let you know that the behavior most likely exists in Android Studio as well. Because of this, it's not considered a Xamarin.Android bug, but rather an issue with fragmentation of how these APIs work with different devices and variables of providers.

For example take a look at the following API:

https://developer.android.com/reference/android/location/LocationManager.html#isProviderEnabled(java.lang.String)

You can lookup the source code for this:

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/location/java/android/location/LocationManager.java#1146

Rather I believe you are running into a limitation with this API rather than a Xamarin issue. This means that the API invoked in Java or C# would be the same and provide the same results based on devices/provider behavior. I had made a quick little note to say if you see any differing results between the behavior of Android Studio and Xamarin.Android, that it would indicate a bug in Xamarin.Android.

More context: https://stackoverflow.com/questions/16748300/locationmanager-isproviderenabledlocationmanager-network-provider-is-not-relia

https://stackoverflow.com/questions/11437327/android-locationmanager-isproviderenabled-always-returns-false

One reason might be:

https://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER

> This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.
Comment 5 diegoxleon 2017-06-23 23:40:33 UTC
fair enough.
Thanks Jon
Comment 6 maulik.sakhare 2018-04-09 11:06:09 UTC
I am also facing the same issue, Status for LocationManager.NetworkProvider responds to location service is enabled or not in phone.

If location sevice is enabled then LocationManager.NetworkProvider returns true, else false.

My device operating system in 6.0.1 Marshmellow
Xamarin forms version is 2.4.0.280