Search code examples
androidlocationbroadcastreceiver

Receive a notification (via BroadcastReceiver) when GPS (or location services) is started/stopped


I tried an approach by using a BroadcastReceiver that listens for PROVIDERS_CHANGED action, but that only tells me when the location settings are turned on/off. I would like to know when the location services are being used to acquire a location by any application. I don't need to know which application is doing it, and I don't care for the location itself. I just want to know whenever some application tries to acquire a location, and when it stops doing that. Is this possible?


Solution

  • I don't know if you really need to be notified via broadcast receiver, but if it is not absolutely required then you can use GpsStatus.Listener :

    Here is how to use it :

    mLocationManager.addGpsStatusListener(new GpsStatus.Listener(){
    
        @Override
        public void onGpsStatusChanged(int event) {
            if(event==GpsStatus.GPS_EVENT_STARTED){
                Log.d(TAG,"Gps Started");
            }else if(event==GpsStatus.GPS_EVENT_STOPPED){
                Log.d(TAG,"Gps Stopped");
            }
        }
    });
    

    And if you really need to receive this info via BroadcastReceiver : just wrap this code in a Service and send the event from there.