LocationManager requestLocationUpdates doesn't work

Dan Dinu picture Dan Dinu · Jun 6, 2012 · Viewed 15.8k times · Source

I'm trying to get the current location in android using LocationManager and LocationListener as described at http://developer.android.com/guide/topics/location/obtaining-user-location.html

However, onLocationChanged method of the LocationListener is never called. I've used a real android phone / also used the emulator and simulated location changed using telnet, as described in the link above.

Here's my code:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Use the LocationManager class to obtain GPS locations */
        LocationManager mlocManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new CustomLocationListener(
                getApplicationContext());
        // Location Providers
        String locationProvider = LocationManager.NETWORK_PROVIDER;
        // LocationProvider locationProvider = LocationManager.GPS_PROVIDER;
        mlocManager
                .requestLocationUpdates(locationProvider, 0, 0, mlocListener);
    }
}

and my LocationListener:

public class CustomLocationListener implements LocationListener {

    private Context m_context;

    public CustomLocationListener(Context context) {
        m_context = context;
    }

    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        String Text = latitude + " " + longitude;
        Toast.makeText(m_context, Text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}

So onLocationChanged is never called. Can anyone see the problem?? Thanks a lot

Answer

CircuitBreaker716 picture CircuitBreaker716 · May 25, 2013

You can also let your phone determine the best provider instead of just setting

String locationProvider = LocationManager.NETWORK_PROVIDER;

Try this instead:

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);   
String locationProvider = mlocManager.getBestProvider(criteria, true);

Also, have you tested by moving your device to a new location? Otherwise the "onlocationchanged" event may not fire. When I was figuring this stuff out a couple years ago, I put in some "toast" to alert me of my lat/long coordinates as I walked around the block to verify the event was firing.