Android: check if GPS is working, fixed or not in use

I want to know if there is a way in Android (GingerBread) to find out if GPS is currently doing something or not. Let me be more clear: I basically want to name some method or api that will tell me that GPS:

1) Fixed (GPS icon in the status bar is on)
2) Search for a fix (GPS icon on the status bar blinks)
3) Inactive (no application currently uses location services, no icon on the status bar)

Now: I know that you can use the LocationListener to notify of these changes, but this is not good for me, because I do not want my code to wait for events, my code runs periodically on a schedule, does something, and then shuts down, so I need a way to check the status of the GPS service at that exact moment, and not wait for notifications when it will change.

+5
source share
2 answers

After much testing on GPS, I found a solution. When the Android application calls the location manager and the GPS starts searching, one event is triggered, and when the gps lock is blocked, another event is triggered. The 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);           
        }
    }

/*    * GPSListener, ,    * GPS , GPS , GPS    */

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;                  
       }
   }
};  

GPS- GPS.

, GPS, GPSStatus. GPS_SEARCHING , GPS , GPS_LOCKED , GPS . GPS, GPS_EVENT_FIRST_FIX , ( GPS_LOCKED), GPS , GPS_SEARCHING (.. GPS_STARTED ). GPS_EVENT_FIRST_FIX GPSstatus.

GPS_EVENT_FIRST_FIX, gpslastknownlocation(), GPS-. ( Android).

, ...

+12

, " " GPS Android. , onGpsStatusChanged() .

, $ dumpsys location adb, gps. $ adb shell , Android exec() , dumpsys .

+1

All Articles