I want to implement a logger, which stores the location of the client after a certain time or when the position is changed. When i am starting the logger, it serves the location only one time. When i am changing the position of the client, nothing happens. The permissions ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, WRITE_EXTERNAL_STORAGE are granted and i am using an emulator with play store installed.
Here is my code:
import android.annotation.SuppressLint;
import android.location.Location;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.Granularity;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.Priority;
import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import de.remi.locker.database.entities.Trip;
import de.remi.locker.util.MapsHelper;
import de.remi.locker.util.PermissionHelper;
public class AndroidLogger implements AutoLogger.ILoggerClient {
private static final String TAG = "AndroidLogger";
private final FragmentActivity activity;
private FusedLocationProviderClient client;
private LocationCallback callBack;
public AndroidLogger(FragmentActivity activity) {
this.activity = activity;
init();
}
private void init() {
client = LocationServices.getFusedLocationProviderClient(activity);
callBack = new LocationCallback() {
@Override
public void onLocationResult(@NonNull LocationResult locationResult) {
locationResult.getLocations().forEach(location -> handleLocation(location));
}
};
}
private void handleLocation(Location location) {
Log.d(TAG, "handleLocation: " + location);
}
private LocationRequest getLocationRequest() {
LocationRequest.Builder builder = new LocationRequest.Builder(10000);
builder.setMinUpdateDistanceMeters(50);
builder.setDurationMillis(60 * 1000);
builder.setGranularity(Granularity.GRANULARITY_FINE);
builder.setWaitForAccurateLocation(false);
builder.setPriority(Priority.PRIORITY_HIGH_ACCURACY);
return builder.build();
}
@SuppressLint("MissingPermission")
@Override
public void startLogging(Trip trip) {
if (!PermissionHelper.hasPermissions(activity.getApplicationContext(), MapsHelper.OSM_PERMISSIONS))
throw new UnsupportedOperationException("permissions missing ");
client.requestLocationUpdates(getLocationRequest(), callBack, Looper.myLooper()).addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
Log.d(TAG, "onCanceled: ");
}
}).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "onComplete: ");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "onFailure: ", e);
}
}).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "onSuccess: ");
}
});
}
@Override
public void stopLogging() {
client.removeLocationUpdates(callBack);
}
In my understanding every 60 seconds or after a position change of minimum 50 meters a new position log should be performed. I used several emulators, from API 30 - 33, changed the CallBack to a listener, nothing changed. Also i changed several parameters of the LocationRequest, without any change
Hope anybody has a helping hint