0

The code below does not work when the application is minimized or closed and doesn't return the current location.

For example, locationCallback only works once and does not work after the app is minimized. It should be noted that this service has been added to the manifest. It works in the video that I saw on YouTube YouTube Link

public class UpdateLocationService extends Service {
 
static final int LOCATION_SERVICE_id = 175;
static final String ACTION_START_LOCATION_SERVICE = "startLocationService";
static final String ACTION_STOP_LOCATION_SERVICE = "stopLocationService";
private LocationCallback locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {

        if (locationResult != null && locationResult.getLastLocation() != null) {
            double lat = locationResult.getLastLocation().getLatitude();
            double lng = locationResult.getLastLocation().getLongitude();
            Log.d(TAG, lat + ", " + lng);

        }
        super.onLocationResult(locationResult);
    }
};

@Nullable
@Override
public IBinder onBind(Intent intent) {

    return null;
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void startLocationService() {
    

    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(3000);
    locationRequest.setFastestInterval(2000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
    }
    LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());

    startForeground(LOCATION_SERVICE_id, builder.build());


}


private void stopLocationService() {
    LocationServices.getFusedLocationProviderClient(this);
    stopForeground(true);
    stopSelf();
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();
        if (action != null)
         if (action.equals(ACTION_START_LOCATION_SERVICE)){
             startLocationService();
         }else if (action.equals(ACTION_STOP_LOCATION_SERVICE)){
             stopLocationService();
         }
    }

    return super.onStartCommand(intent, flags, startId);

}

}

Alias Cartellano
  • 366
  • 1
  • 3
  • 12
  • At the very least you'll need to request the `ACCESS_BACKGROUND_LOCATION` permission on API 29 and higher. – Daniel Nugent Sep 13 '21 at 23:50
  • This permission is in the my manifest – vahid asghari Sep 14 '21 at 05:33
  • It's not enough to just have it in the manifest, you also need to request the permission at runtime. – Daniel Nugent Sep 14 '21 at 16:18
  • i check permission in service with this condition but it does not works again and returned true. if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) – vahid asghari Sep 14 '21 at 17:33
  • Use this in your activity class to request the permissions: `ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);` – Daniel Nugent Sep 14 '21 at 17:47
  • I do this and I got the necessary permissions – vahid asghari Sep 14 '21 at 17:57
  • 1
    If you use a service, you will likely need to use a ForegroundService: https://developer.android.com/guide/components/foreground-services – Daniel Nugent Sep 14 '21 at 18:01
  • tnx dude, solved with append android:permission="android.permission.ACCESS_BACKGROUND_LOCATION" and android:foregroundServiceType="location" to service in manifest – vahid asghari Sep 14 '21 at 18:33

1 Answers1

0

Starting background service has been restricted after Android O https://developer.android.com/about/versions/oreo/background. If you still what to get a location in the background, please consider WorkManger and Foreground services

Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22