1

I'm trying to write an Android app that gets the current location from the 'network' provider (so I should only need the android.permission.ACCESS_COARSE_LOCATION permission). This works fine on some devices, but on others I don't seem to be able to get a location update. Here's my code.

In onCreate:

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = LocationManager.NETWORK_PROVIDER;
    Log.v (TAG, "Finding location from provider: " + provider);
    if (CHECK_PROVIDER_ENABLED && !locationManager.isProviderEnabled (provider))
    {
        Toast.makeText (this, "Location provider disabled", Toast.LENGTH_LONG).show ();
    }
    else
    {
        try
        {
            Location location = locationManager.getLastKnownLocation(provider);
            if (location == null)
            {
                Log.i (TAG, "getLastKnownLocation returned null, requesting updates when available");
                locationManager.requestLocationUpdates (provider, 0, 0, locationListener);
            }
            else
                locationListener.onLocationChanged (location);
        }
        catch (IllegalArgumentException e)
        {
            Log.e(TAG, "Failed to get location from provider " + provider, e);
            Toast.makeText (this, "Location provider disabled", Toast.LENGTH_LONG).show ();
        }
    }

locationListener is defined as follows:

private LocationListener locationListener = new LocationListener() {

    @Override
    public void onStatusChanged (String provider, int status, Bundle extras)
    {
    }

    @Override
    public void onProviderEnabled (String provider)
    {
    }

    @Override
    public void onProviderDisabled (String provider)
    {
    }

    @Override
    public void onLocationChanged (Location location)
    {
        Toast.makeText (CarouselViewerActivity.this, "" + location.getLatitude () + ", " + location.getLongitude (), Toast.LENGTH_LONG).show ();
        Log.v(TAG, "" + location.getLatitude () + ", " + location.getLongitude ());
        locationManager.removeUpdates (this);
    }
};

On phones that work, everything here works fine: if CHECK_PROVIDER_ENABLED is true, the isProviderEnabled test returns true, getLastKnownLocation returns a location rather than null, and if I force the request for updates I receive one with details of a location.

But on other phones (the specific one I'm testing with is a Galaxy S2 with Cyanogenmod 9) the isProviderEnabled test returns false (despite the settings item for enabling network location being turned on - see LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is not reliable, why? for a summary of this issue), getLastKnownLocation returns null, and after requesting updates I do not receive one in a reasonable time (I've waited 5 minutes).

Is there something wrong with the approach I'm taking? Is there some other way of getting the network location I need, or some setting on the phone other than enabling network location that's required for this to work? At a more basic level, what exactly is going wrong here?

Community
  • 1
  • 1
Jules
  • 14,841
  • 9
  • 83
  • 130
  • Did you check if it is not a bug with that particular version of cyanogenmod 9 and the galaxy s2? Does the gps work on other apps like maps? There are other explicit ways to let add the gps feature to the xml that might help. All that considered this does seem like a problem with this specific rom and phone. – Gabriel Netto Aug 12 '13 at 20:10
  • Other applications are definitely able to make it work, so I don't think it's a problem that's that specific, and I'm sure there must be a solution. – Jules Aug 12 '13 at 20:12

1 Answers1

1

Similar issues relating to the Android Network Provider have been reported before on Stack Overflow which you can find here. The consensus from those who have experienced this problem state that there are two possible workarounds, rebooting the device when the issue becomes apparent, or by utilizing the Google Play Service API for retrieving location data as there appears to be an issue with the reliability of the Location Manager with Android 4.0 or newer.

Community
  • 1
  • 1
Nexuz
  • 192
  • 12
  • 1
    I'm not aware of any official bug report from Google but you can check the Android Open Source Project [bug tracker](https://code.google.com/p/android/issues/list). I have also tested the following location based [tutorial](http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) on Android 4.2 and 4.3 without issue. This may be of interest as it deals with location management slightly differently to yourself. – Nexuz Aug 12 '13 at 20:33
  • 1
    It would appear to be this bug: https://code.google.com/p/android/issues/detail?id=57707 – Jules Aug 12 '13 at 21:26
  • 1
    This is indeed a bug as it's affecting me as well. I implemented the location services found in Google Play Service API and that resolved it. I also like the advanced features that come with that API so I probably would have moved to that eventually anyway. – Daniel Eagle Oct 06 '14 at 19:12