2

I have implemented a LocationListener to receive regular updates in a service when app is minimized. However this does not work. Updates are only received when the app is visible. Location updates cease when screen is locked or app is minimized.

public class MapService1 extends Service {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;
protected LocationManager locationManager;

Location location;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

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

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener());
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

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

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
        System.out.println("----------------------NEW ON LOCATION CHANGED (" + location.getLatitude() + "," + location.getLongitude() + ")----------------------");
    }

    public void onStatusChanged(String s, int i, Bundle b) {
    }

    public void onProviderDisabled(String s) {
    }

    public void onProviderEnabled(String s) {
    }
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Add this permission to your manifest file

    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Your problem is likely that your app only has permission to location while using the app. This will give you the option to accept location permissions all the time.

edgar_wideman
  • 315
  • 4
  • 16