I want to fetch accurate position in native after every 40 seconds but it is not working. I want to implement location update in background here is my native code
private void processLocation(Location location) {
double currentLat = location.getLatitude();
double currentLng = location.getLongitude();
double accuracy = location.getAccuracy();
Log.i(TAG, "Current Location: Lat: " + currentLat + ", Lng: " + currentLng);
long currentTimeMillis = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTimeMillis);
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
int currentMinute = calendar.get(Calendar.MINUTE);
// Process each employee/task
for (HashMap<String, Object> task : empData) {
double officeLat = (double) task.get("office_lat");
double officeLng = (double) task.get("office_long");
double cabinLat = (double) task.get("cabin_lat");
double cabinLng = (double) task.get("cabin_long");
int id = (int) task.get("id");
// Calculate distance to office
float[] results = new float[1];
Location.distanceBetween(currentLat, currentLng, officeLat, officeLng, results);
float distanceInMeters = results[0];
// Calculate distance to cabin
Location.distanceBetween(currentLat, currentLng, cabinLat, cabinLng, results);
float cabinDistance = results[0];
// Retrieve timing parameters from task
int officeCheckInTime = (int) task.get("office_checkin_time");
int graceTime = (int) task.get("grace_time");
int checkOutTime = (int) task.get("office_checkout_time");
int breakOutTime = (int) task.get("break_out_time");
// --- Office Check-In/Check-Out Logic ---
if (distanceInMeters <= 50) { // User is within office radius
// Office check-in
// if (!checkInStatus.getOrDefault(id, false)) {
int totalMinutesLate = (currentHour * 60 + currentMinute) - (officeCheckInTime * 60);
if (totalMinutesLate <= graceTime) {
sendNotification("Check-in successful",
"Check-in Successful. Distance: " + distanceInMeters + " meters.", id + 7643, this);
checkInStatus.put(id, true);
} else {
String lateTime = formatTime(totalMinutesLate - graceTime);
sendNotification("Check-in successful",
"Check-in Late by " + lateTime + ". Distance: " + distanceInMeters + " meters.", id + 7643,
this);
sendNotification("Accuracy",
"Accuracy is" + accuracy, id + 435,
this);
checkInStatus.put(id, true);
}
// }
// --- Cabin Break Logic ---
if (checkInStatus.getOrDefault(id, false)) {
if (cabinDistance > 10) { // User is outside cabin radius
if (!lastBreakTime.containsKey(id)) {
// Start break timer
lastBreakTime.put(id, System.currentTimeMillis());
sendNotification("Break Started",
"Your Break has Started. Cabin Distance: " + cabinDistance + " meters.", id + 1122,
this);
Log.i(TAG, "User left cabin, starting break timer.");
} else {
// Check if break duration exceeds the allowed break-out time
long breakDuration = (System.currentTimeMillis() - lastBreakTime.get(id)) / 60000; // Convert
// ms to
// minutes
if (breakDuration >= breakOutTime) {
if (!checkOutStatus.getOrDefault(id, false)) {
sendNotification("Early Checkout",
"You left your cabin for " + breakOutTime + " minutes. Auto check-out.",
id + 1122, this);
checkOutStatus.put(id, true);
Log.i(TAG, "User auto checked out due to long absence from cabin.");
}
}
}
} else {
// User is within cabin radius
if (lastBreakTime.containsKey(id)) {
long breakDuration = (System.currentTimeMillis() - lastBreakTime.get(id)) / 60000;
if (breakDuration < breakOutTime) {
lastBreakTime.remove(id);
sendNotification("Break Ended",
"You returned to your cabin. Break timer reset.", id + 1122, this);
Log.i(TAG, "User returned to cabin, break timer reset.");
}
}
}
}
// Office check-out (if within office radius)
if (!checkOutStatus.getOrDefault(id, false)) {
int totalMinutesLateCheckout = (currentHour * 60 + currentMinute) - (checkOutTime * 60);
if (totalMinutesLateCheckout == 0) {
sendNotification("Check-out successful", "Check-out Successful.", id + 3324, this);
checkOutStatus.put(id, true);
}
} else {
Log.i(TAG, "User has already checked out.");
}
} else {
// --- Outside Office Radius Check-Out ---
if (!checkOutStatus.getOrDefault(id, false)) {
int totalMinutesEarly = (checkOutTime * 60) - (currentHour * 60 + currentMinute);
if (totalMinutesEarly > 0) {
String earlyTime = formatTime(totalMinutesEarly);
sendNotification("Check-out successful",
"You left " + earlyTime + " before time. Distance: " + distanceInMeters + " meters.",
id + 3243, this);
checkOutStatus.put(id, true);
}
} else {
Log.i(TAG, "User has already checked out.");
}
}
}
}
I am sending data through platform channels I am also using flutter geolocator but it is not functioning as desired here is my platform channel code
Future<void> sendTasksToNative() async {
try {
const LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
Position position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings);
var accuracy = await Geolocator.getLocationAccuracy();
print(
"Current Latitude:::::::::::::: ${position.latitude.toString()} Current Longitude:::::::::::::: ${position.longitude.toString()} :::::::::::::: Accueracy : $accuracy");
var empList = [
{
"id": 1,
"company_name": "Softlinks",
"emp_name": "Abdullah Shamoon",
"break_time": 13,
"break_out_time": 10,
"office_checkin_time": 10,
"office_checkout_time": 19,
"grace_time": 10,
"office_lat": position.latitude,
"office_long": position.longitude,
"cabin_lat": position.latitude,
"cabin_long": position.longitude
}
];
List<Map<String, dynamic>> empData =
empList.toList().cast<Map<String, dynamic>>();
await platform.invokeMethod('sendData', {
'emp_data': empData,
});
print(
"All Employee Data Sent From flutter to Native: ${empData.toString()}");
} on PlatformException catch (e) {
print("Failed to send tasks to native: '${e.message}'.");
}
}