Adding lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
it will start fetching the location using network but once you come out from the Tunnel, it will start fetching from Network and GPS. Using location.getProvider()
you should be able to differentiate to know whether it's gps or network.
If your GPS status isn't changing (e.g., if you're always indoors without a GPS fix) while the app is running, some devices won't trigger the OnStatusChanged()
method.
If you change GPS statuses while the app is running (e.g., you're inside and can't get a fix and then walk outside and can get a fix, or vice versa), then the OnStatusChanged()
method should fire on all devices.
If you want a fully working open-source app to use as an example, try GPSTest by Mike Lockwood from the Android team.
GPSTest on Google Play
Source code for GPSTest
For more detailed information about GPS that is constantly updated even if your device can't get a fix, you might want to register a GPSStatusListener.
In your Activity, declare class variables:
private LocationManager mService;
private GpsStatus mStatus;
...and add the method to handle the GPSStatus changes:
public void onGpsStatusChanged(int event) {
mStatus = mService.getGpsStatus(mStatus);
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
// Do Something with mStatus info
break;
case GpsStatus.GPS_EVENT_STOPPED:
// Do Something with mStatus info
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
// Do Something with mStatus info
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// Do Something with mStatus info
break;
}
}
Then in OnCreate() of your Activity to register the GPSStatusListener:
mService = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mService.addGpsStatusListener(this);
In the GPSTest app, the list of currently available satellites is shown on the screen with each GPSStatusListener update, based on this code.
This way, you'll receive active updates on the GPS status of system even if your phone can't get a GPS fix (and therefore may not trigger OnStatusChanged
of the LocationListener
).