I try to ask the location with fusedLocationClient, the thread is well launching, I can see the Log.d("TAG", "Foreground Service is running"); but the problem is that onLocationResult is not fired...
public int onStartCommand(Intent intent, int flags, int startId) {
shouldContinueLocationUpdates = true;
if (thread == null) {
thread = new Thread(new Runnable() {
@Override
public void run() {
while (shouldContinueLocationUpdates) {
Log.d("TAG", "Foreground Service is running");
fusedLocationClient3 = LocationServices.getFusedLocationProviderClient(getApplicationContext());
locationCallback3 = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
Location location = locationResult.getLastLocation();
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// Utilisez la latitude et la longitude ici
sendLocationUpdateBroadcast(latitude, longitude);
}
}
};
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.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);
}
How to get the onLocationResult fired?