3

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?

ben75
  • 29,217
  • 10
  • 88
  • 134
Omar Bahareth
  • 875
  • 6
  • 22
  • I am interested to know this too – alandalusi May 02 '14 at 18:07
  • See this [link](http://stackoverflow.com/questions/11398732/how-do-i-receive-the-system-broadcast-when-gps-status-has-changed)..Tell me if it helped... @alandalusi and Xen – Lal May 18 '14 at 06:37
  • Check this [link](http://stackoverflow.com/questions/20673620/how-to-trigger-broadcast-receiver-when-gps-is-turn-on-off) too... – Lal May 18 '14 at 06:38
  • @Lal I looked at them before and I did use PROVIDERS_CHANGED like I said in my question. The first example was using the unofficial android.location.GPS_ENABLED_CHANGE (and I need to listen for location services in general, not just GPS), and that doesn't seem to work on all devices. The answer to it along with the other link both use PROVIDERS_CHANGED, and that isn't telling me when location services are turned on/off. It's telling me when the user enables/disables the location service setting. – Omar Bahareth May 19 '14 at 07:45
  • Actually just GPS will do for now, but I need an approach that works everywhere. – Omar Bahareth May 19 '14 at 08:45

2 Answers2

6

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.

ben75
  • 29,217
  • 10
  • 88
  • 134
  • I will test to see if this is reliable. I have to make it run in the background on my own and recreate it if necessary. – Omar Bahareth May 20 '14 at 16:55
  • Worked fine. I read somewhere that you had to start listening for location updates before using a GpsStatus.Listener, but it turns out that wasn't the case. This actually worked, and I wrapped it in a sticky service which gets restarted on boot. Thank you @ben75 – Omar Bahareth May 21 '14 at 18:31
  • this is not working for me, i have put this in my fragment's onCreateView() – Kaveesh Kanwal Jun 07 '16 at 12:23
  • Unfortunately, GpsStatusListener is deprecated nowadays. – Jesús Castro Jul 19 '16 at 14:23
4

Not precisely what you asked for but might be of interest: you can register for location updates from PASSIVE_PROVIDER through LocationManager.

This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself.

You won't be able to know when other apps stop requesting new locations, or when they start exactly, but you'll know when they receive one that they have requested.

Nathanael
  • 701
  • 5
  • 12