6

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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Dan Dinu
  • 32,492
  • 24
  • 78
  • 114
  • Have you enabled the GPS services on your phone and have you confirmed that you should have a location? (Waiting a few minutes is not a confirmation, if you are in a poor service area 30 minutes may not be enough...) – Sam Jun 06 '12 at 20:44
  • Gotta ask: do you have the location permissions specified in the manifest? – dymmeh Jun 06 '12 at 20:47
  • @dymmeh yes, i've added permission in the manifest – Dan Dinu Jun 07 '12 at 11:02
  • @Sam i undestood that NETWORK_PROVIDER doens't use phone's gps – Dan Dinu Jun 07 '12 at 11:03
  • @DanDinu I'm sorry, I read your question wrong. As you know, the Network location does not need the GPS service enabled, it needs the Network Provider enabled. Any luck solving your problem? Your code here looks fine. – Sam Jun 07 '12 at 23:05

4 Answers4

4

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.

3

This looks like Android Bug 57707 to me.

If it is, rebooting the device should help, at least for a while.

Or you could use the Google Play Services Location API.

By using both Wifi, GPS and Network positioning, the Google Play Services Location API will hide the fact that Network positioning is broken; that will only be obvious when you have neither GPS nor Wifi available.

Johan Walles
  • 1,447
  • 2
  • 15
  • 23
1

Make sure that the proper location services are enabled in Settings. You can also use isProviderEnabled() to check if the particular provider you've chosen will work. And ensure you have the COARSE or FINE location permissions in your manifest.

Also, make sure you're testing on a device. Network location does not work in the emulator at all, and GPS will only work if you use the simulation tool to inject new data points.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Unfortunately, `isProviderEnabled()` doesn't work as advertised. See: http://stackoverflow.com/questions/10154937/isproviderenabledlocationmanager-network-provider-return-false and http://stackoverflow.com/questions/16748300/locationmanager-isproviderenabledlocationmanager-network-provider-is-not-relia – Jules Aug 12 '13 at 10:21
0

I have faced a similar problem. Try replacing getApplication() with MainAvtivity.this

Also, make sure you are keeping permissions for both FINE and COARSE locations. If your target API is more than 23, you should handle permissions from java code also.

Akhil Ghatiki
  • 1,140
  • 12
  • 29