0

I'm writing an app that requests for location updates like this:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval, 0, locationListener);

onLocationChanged gets invoked when device is on but when put into sleep it stops receiving further updates. I followed this thread but as I said - it doesn't work.

The listener is registered inside foreground service that needs to work all the time. I want to conserve battery by waking it from sleep only when I receive new location.

EDIT: of course everything works when I acquire wakelock in onCreate() method and release it in onDestroy() method for service but I don't want to do this.

Community
  • 1
  • 1
Waszker
  • 233
  • 3
  • 10
  • have you try https://github.com/nickfox/GpsTracker/blob/master/phoneClients/android/app/src/main/java/com/websmithing/gpstracker/LocationService.java – Shayan Pourvatan Dec 21 '14 at 12:34
  • @shayanpourvatan I haven't tried it. It looks just like normal registering for location updates but from GooglePlayServices not LocationManager. Are you sure this will resolve my issues? – Waszker Dec 21 '14 at 12:44
  • no , i'm not sure, i see that link in http://stackoverflow.com/questions/14478179/background-service-with-location-listener-in-android, – Shayan Pourvatan Dec 21 '14 at 12:46

1 Answers1

0

If you want to use background task then use this service

public class LocationFinder extends Service {
    public static double lat, lng;

    LocationManager locationManager;

    public void onDestroy() {

        super.onDestroy();

        if (locationManager != null && locationListener != null) {
            locationManager.removeUpdates(locationListener);
        }

    }

    @Override
    public void onCreate() {
        Log.v("location", "===>location ed  onCreate " + lat);

        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        Log.v("location", "===>location ed  onStartCommand " + lat + "==>" + lng);
        new Handler().post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                getLocation();

            }
        });
        return START_STICKY;
    }

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    private void getLocation() {
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                Location location = locationManager.getLastKnownLocation(provider);
                updateWithNewLocation(location);
                locationManager.requestLocationUpdates(provider, 500, 50, locationListener);
            } else {
                if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);

                } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener);
                } else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener);
                }
            }

        }
    }

    private void updateWithNewLocation(Location location) {
        if (location != null) {
            Log.v("location", "===>location ed  " + lat);

            lat = location.getLatitude();
            lng = location.getLongitude();

        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138