7/22/1 location background

This commit is contained in:
Hamza-Ayed
2024-07-22 20:08:42 +03:00
parent d6410e45a4
commit dea83d970c
8 changed files with 95 additions and 54 deletions

View File

@@ -8,13 +8,19 @@ class LocationBackgroundController extends GetxController {
void onInit() {
super.onInit();
requestLocationPermission();
configureBackgroundLocation();
}
Future<void> requestLocationPermission() async {
var status = await Permission.locationAlways.status;
if (!status.isGranted) {
await Permission.locationAlways.request();
status = await Permission.locationAlways.request();
}
if (status.isGranted) {
configureBackgroundLocation();
} else {
// Handle permission denial
print("Location permission denied");
}
}
@@ -25,29 +31,36 @@ class LocationBackgroundController extends GetxController {
icon: "@mipmap/ic_launcher",
);
BackgroundLocation.setAndroidConfiguration(1000);
// Set the location update interval to 5 seconds
BackgroundLocation.setAndroidConfiguration(5000);
BackgroundLocation.startLocationService();
BackgroundLocation.getLocationUpdates((location) {
// Handle location updates here
print("Latitude: ${location.latitude}, Longitude: ${location.longitude}");
});
startBackLocation();
}
void startBackLocation() async {
Timer.periodic(const Duration(seconds: 5), (timer) async {
await getBackgroundLocation();
});
}
startBackLocation() async {
Timer.periodic(const Duration(seconds: 5), (timer) {
getBackgroundLocation();
});
}
getBackgroundLocation() async {
Future<void> getBackgroundLocation() async {
var status = await Permission.locationAlways.status;
if (status.isGranted) {
await BackgroundLocation.startLocationService(
distanceFilter: 20, forceAndroidLocationManager: true);
BackgroundLocation.setAndroidConfiguration(
Duration.microsecondsPerSecond); // Set interval to 5 seconds
BackgroundLocation.getLocationUpdates((location1) {});
// The location service is already started in configureBackgroundLocation
// No need to call startLocationService again
BackgroundLocation.getLocationUpdates((location) {
// Handle location updates here
print(
"Latitude: ${location.latitude}, Longitude: ${location.longitude}");
});
} else {
// Request permission if not granted
await Permission.locationAlways.request();
}
}