Reputation: 6196
I'm trying to get notifications if the status of GPS_PROVIDER changes. I found the following code here (http://hejp.co.uk/android/android-gps-example/), but I'm not getting notifications.
Thanks,
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.v(tag, "Status Changed: Out of Service");
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v(tag, "Status Changed: Temporarily Unavailable");
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.v(tag, "Status Changed: Available");
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
Upvotes: 3
Views: 25190
Reputation: 2181
Android documentation says that onStatusChanged
method is just deprecated:
This callback will never be invoked on Android Q and above, and providers can be considered as always in the LocationProvider#AVAILABLE state.
But I found out that it still needs to be implemented even though Android Studio doesn't complain about it, otherwise the app will crash
Upvotes: 0
Reputation: 11756
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 (full disclosure, my app):
GPSTest on Google Play - https://play.google.com/store/apps/details?id=com.android.gpstest
Source code for GPSTest - https://github.com/barbeau/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 GPSStatus.Listener
.
In your Activity, make it implement GpsStatus.Listener
, for example:
public class GpsTestActivity extends TabActivity
implements LocationListener, GpsStatus.Listener{
Then, 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 GPSStatus.Listener:
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 GPSStatus.Listener 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).
Upvotes: 22
Reputation: 1631
I worked on the GPS for past several week and whatever I noticed from my testing, there could be a possibility to get the GPS signals inside a building. If you are in a basement area, then you will never get. I tried from my home and 95% of time get my latitude and longtitude always.
You can't control this. GpsLocationProvider automatically uses assisted SUPL if there is an internet or WiFi connection available. The standalone case is only used when internet isn't accessible. Assisted data help in locking on to the GPS satellite signal. In indoor cases, you can see 2-3 satellites (it depends on the quality of your chipset). In case no satellite signal can be scanned, you can't get a fix.
Thanks, Ramesh S
Upvotes: 0
Reputation: 2705
maybe you need to check this link GpsStatus.Listener this listener is used to know when GPS status has changed.
and as for the method you pointed, it is used to know when the provider status changed as mentioned in the documentation LocationListener
Upvotes: 1