Using LocationListener for API 21 on Marshmallow

636 views Asked by At

I have an app that listens to the GPS update, which was implemented last year using API 21. It was tested on several Android phones running Lollipop, such as LG G4. Now I installed it on an LG G5 with Marshmallow and I receive no GPS updates, i.e. onLocationChanged() is never called.

The permissions are granted (the App view on the phone's settings shows Location permission) and the following statement is true

checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED

AndroidManifest.xml includes these lines:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />

The code I am using is as below:

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

locationListener = getLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


private LocationListener getLocationListener() {
        LocationListener locationListener = new LocationListener() {
           public void onLocationChanged(Location location) {
                 handleLocationChange();
           }
           public void onStatusChanged(String provider, int status, Bundle extras) { }

           public void onProviderEnabled(String provider) { }

           public void onProviderDisabled(String provider) { }
    };
    return locationListener;
}

I tried NETWORK_PROVIDER as well, no success.

What am I missing here?

1

There are 1 answers

0
Onur On BEST ANSWER

I realized that the GPS Sensor is not providing any updates to the phone. Such as, when I restrict the Location precision to the GPS Sensor only, Google Maps never get a location fix.

If I connect the phone to the internet, then the NETWORK_PROVIDER provides location updates.