xenonite
xenonite

Reputation: 1671

Listen *passively* to gps location updates

What I want to do is the following: a service, that continuously listens for gps (yes: JUST gps) location updates without actively requesting them - i.e. this app should consume no additional resources in terms of battery life or cpu time, until other apps request/receive gps location updates. THEN my app should execute some functionality. after this, it should go back to "sleep", waiting for the next gps position fix to be requested by some other app...

I already read this article http://android-developers.blogspot.de/2011/06/deep-dive-into-location.html but this seems to have a different focus. actually, I'm a bit lost here... how would one use this passive location provider exactly? I cannot get this to work.

My code so far: I have this (remote) service that registers the location listener:

public class MyService extends Service
{
    LocationManager locationManager;

    @Override
    public IBinder onBind(Intent intent)
    {
        return serviceBinder;
    }

    private final IMyService.Stub serviceBinder = new IMyService.Stub()
    {};

    @Override
    public void onCreate()
    {
        super.onCreate();

        Log.d(Config.LOGTAG, "service started");

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, new GPSLocationListener());
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();

        Log.d(Config.LOGTAG, "service destroyed");
    }

    public class GPSLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location location)
        {
            Toast.makeText(getBaseContext(), "GPS FIX RECEIVED: " + location.getLongitude() + " : " + location.getLatitude(), Toast.LENGTH_LONG).show();
            Log.d(Config.LOGTAG, "GPS FIX RECEIVED: " + location.getLongitude() + " : " + location.getLatitude());
        }

        @Override
        public void onProviderDisabled(String arg0)
        {
            Log.d(Config.LOGTAG, ":(");
        }

        @Override
        public void onProviderEnabled(String arg0)
        {
            Log.d(Config.LOGTAG, ":)");
        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2)
        {
            Log.d(Config.LOGTAG, ":/");
        }
    }
}

Unfortunately, this is not working at all: The service is started according to the logs, but when I e.g. open google maps with gps enabled and get a position fix there, the location listener is not "notified" (i.e. does not log anything)

How to do this right?

Upvotes: 4

Views: 4661

Answers (3)

serv-inc
serv-inc

Reputation: 38147

How about using Google (or Lost) LocationRequest (code extended from https://github.com/lostzen/lost/blob/master/docs/location-updates.md)

LocationRequest request = LocationRequest.create()
    .setPriority(LocationRequest.PRIORITY_NO_POWER)
    .setInterval(5000)
    .setSmallestDisplacement(10);

LocationListener listener = new LocationListener() {
  @Override
  public void onLocationChanged(Location location) {
    if (location.getProvider().equals("gps")) {
      // Do stuff
    }
  }
};

As of https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_NO_POWER, this gives the

best accuracy possible with zero additional power consumption.

No locations will be returned unless a different client has requested location updates in which case this request will act as a passive listener to those locations.

Upvotes: 0

user1177820
user1177820

Reputation:

Hey I think you need updated longitude and latitude values .

follow this link.. Click here

I already tried it and test it in android mobile phone.. Its working.

Note : Must and should test in android mobile phone with on GPS status. It will not show on eclipse emulator. But it will show in mobile phone try it..

have a nice.

Upvotes: 1

Ollie C
Ollie C

Reputation: 28509

I'd want to know whether it's not working because no passive locations are being received, or the code's not working. So the first thing I'd do is start listening for network and GPS locations (for testing) and make sure they arrive. If they do, then that suggests Google Maps is using a cached location and is not instigating a passive location broadcast.

If you don't get any locations, you need to check your code and get it working with GPS/network, before moving on to passive locations.

Upvotes: 1

Related Questions