How to Check the Current Status of the Gps Receiver

Android: check if GPS is searching, has fix or is not in use

After doing lot of testing on GPS, finally I found the solution. When android app calls location manager and GPS starts searching, one event is triggered and also when gps is locked another event is triggered. Following code shows how to do this.

locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
if (locationManager != null) {
// Register GPSStatus listener for events
locationManager.addGpsStatusListener(mGPSStatusListener);
gpslocationListener = new LocationListener() {
public void onLocationChanged(Location loc) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES_GPS, MIN_DISTANCE_CHANGE_FOR_UPDATES_GPS,
gpslocationListener);
}
}

/*
* This is GPSListener function invoked when various events occurs like
* GPS started, GPS stopped, GPS locked
*/

public Listener mGPSStatusListener = new GpsStatus.Listener() {  
public void onGpsStatusChanged(int event) {
switch(event) {
case GpsStatus.GPS_EVENT_STARTED:
Toast.makeText(mContext, "GPS_SEARCHING", Toast.LENGTH_SHORT).show();
System.out.println("TAG - GPS searching: ");
break;
case GpsStatus.GPS_EVENT_STOPPED:
System.out.println("TAG - GPS Stopped");
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:

/*
* GPS_EVENT_FIRST_FIX Event is called when GPS is locked
*/
Toast.makeText(mContext, "GPS_LOCKED", Toast.LENGTH_SHORT).show();
Location gpslocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(gpslocation != null) {
System.out.println("GPS Info:"+gpslocation.getLatitude()+":"+gpslocation.getLongitude());

/*
* Removing the GPS status listener once GPS is locked
*/
locationManager.removeGpsStatusListener(mGPSStatusListener);
}

break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// System.out.println("TAG - GPS_EVENT_SATELLITE_STATUS");
break;
}
}
};

It is better to put GPS code in and as service to get GPS location information.

Each time if you call GPS function, GPSStatus listener is registered. GPS_SEARCHING toast comes only once when GPS is started to search and GPS_LOCKED toast displays when GPS is locked. If we call GPS function again, GPS_EVENT_FIRST_FIX event is triggered if it is locked(displays GPS_LOCKED toast) and if GPS is already started to search it won't display GPS_SEARCHING toast(i.e GPS_STARTED event won't trigger). After GPS_EVENT_FIRST_FIX event is triggered i'm removing the GPSstatus listener updates.

When GPS_EVENT_FIRST_FIX event is triggered, its better to call gpslastknownlocation() function to get fresh latest GPS fix.(Its better to look into Android developers site for more info).

I hope this will help others....

How do I get the GPS fix status on android - right now (not using the listener)

I don't want to listen to events - just to get an answer

You can use getLastKnownLocation() but if this returns null then you must wait for a location update.


To test if the GPS is active use isProviderEnabled().

You should consider using the PASSIVE_PROVIDER as well. This report any location fix whether it's GPS, NETWORK, etc.

How to Find GPS Signal is Lost in Android?

Android and iphone both have a stand still filter enabled, which as you described, leads to the situation that you don't get a location update when standing still.

I would just ignore that, and use the Location.accuracy value for each new location received. If that values exceeds 30m then it is bad GPS.

While standing you could decide to do nothing.

Theoretically there is a solution for your task, but with drawbacks:

When you don't get a new location for some time, you can disable and reenable Location Service every 5 seconds. Then in case of good GPS you get a new coordinate, or none if there is no GPS signal available.



Related Topics



Leave a reply



Submit