I have my fusedlocationprovider that works perfectly, for the distance and for the time when the person is not moving.
function with my FusedLocationProvider:
private void startLocationUpdates() {
// Demander les mises à jour de position
fusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
if (fusedLocationClient != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
//Distance
if (locationRequest == null) {
locationRequest = new LocationRequest();
if (drivingtype == Constants.DRIVING_TYPE_BICYCLE) {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY );
locationRequest.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
locationRequest.setInterval(0);
locationRequest.setFastestInterval(0);
} else {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY );
locationRequest.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
locationRequest.setInterval(0);
locationRequest.setFastestInterval(0);
}
}
if (locationCallback == null) {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// Traitement des mises à jour de localisation
// La position a changé. Obtenez les coordonnées et mettez à jour la carte.
sendLocationUpdateBroadcast(location.getLatitude(), location.getLongitude());
}
}
};
}
Looper mainLooper = Looper.getMainLooper();
if (!locationUpdatesEnabled) {
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, mainLooper);
locationUpdatesEnabled = true;
}
}
}
I wrote that service class to make my app work in background, which is firing perfectly during the background, or when the phone is in standby, with the screen light off.
service class:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Technique #2
new Thread(
new Runnable() {
@Override
public void run() {
while (true){
Log.d("TAG", "Foreground Service is running");
startLocationUpdates();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}
).start();
final String CHANNEL_ID = "Foreground Service";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this, CHANNEL_ID)
.setContentText("Foreground Service Running")
.setContentTitle("This is Title");
startForeground(1001, notification.build());
return super.onStartCommand(intent, flags, startId);
}
What I want, is to get my service launching in background only when the phone is in standby, and fire the onLocationResult(LocationResult locationResult) of the distance fusedlocationprovider when the people is moving, I precise again, only when the app is not at first plan, in background, on another app, or simply when the phone is in standby.
How to realise that ?