Fix background task execution
This commit is contained in:
parent
b907ff1e82
commit
fc4cb80b74
@ -148,12 +148,10 @@ class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateDeviceLocationIsolate() {
|
void updateDeviceLocationIsolate() {
|
||||||
workManager.Workmanager.executeTask((backgroundTask, data) {
|
workManager.Workmanager.executeTask((backgroundTask, data) async {
|
||||||
Completer completer = Completer();
|
|
||||||
print("[Background $backgroundTask] Started");
|
print("[Background $backgroundTask] Started");
|
||||||
Geolocator geolocator = Geolocator();
|
Geolocator geolocator = Geolocator();
|
||||||
var battery = Battery();
|
var battery = Battery();
|
||||||
int batteryLevel = 1;
|
|
||||||
String webhookId = data["webhookId"];
|
String webhookId = data["webhookId"];
|
||||||
String httpWebHost = data["httpWebHost"];
|
String httpWebHost = data["httpWebHost"];
|
||||||
if (webhookId != null && webhookId.isNotEmpty) {
|
if (webhookId != null && webhookId.isNotEmpty) {
|
||||||
@ -165,37 +163,27 @@ void updateDeviceLocationIsolate() {
|
|||||||
"data": {
|
"data": {
|
||||||
"gps": [],
|
"gps": [],
|
||||||
"gps_accuracy": 0,
|
"gps_accuracy": 0,
|
||||||
"battery": batteryLevel
|
"battery": 100
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
print("[Background $backgroundTask] Getting battery level...");
|
print("[Background $backgroundTask] Getting battery level...");
|
||||||
battery.batteryLevel.then((val) => data["data"]["battery"] = val).whenComplete((){
|
int batteryLevel = await battery.batteryLevel;
|
||||||
print("[Background $backgroundTask] Battery level is ${data["data"]["battery"]}. Getting device location...");
|
if (batteryLevel != null) {
|
||||||
geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high, locationPermissionLevel: GeolocationPermission.locationAlways).then((location) {
|
data["data"]["battery"] = batteryLevel;
|
||||||
if (location != null) {
|
}
|
||||||
|
Position location = await geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high, locationPermissionLevel: GeolocationPermission.locationAlways);
|
||||||
|
if (location != null && location.latitude != null) {
|
||||||
print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude}");
|
print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude}");
|
||||||
data["data"]["gps"] = [location.latitude, location.longitude];
|
data["data"]["gps"] = [location.latitude, location.longitude];
|
||||||
data["data"]["gps_accuracy"] = location.accuracy;
|
data["data"]["gps_accuracy"] = location.accuracy;
|
||||||
print("[Background $backgroundTask] Sending data home.");
|
print("[Background $backgroundTask] Sending location data home.");
|
||||||
http.post(
|
http.post(
|
||||||
url,
|
url,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: json.encode(data)
|
body: json.encode(data)
|
||||||
);
|
);
|
||||||
completer.complete(true);
|
|
||||||
} else {
|
|
||||||
print("[Background $backgroundTask] Can't get device location. Location is null");
|
|
||||||
completer.complete(true);
|
|
||||||
}
|
}
|
||||||
}).catchError((e) {
|
|
||||||
print("[Background $backgroundTask] Error getting current location: ${e.toString()}");
|
|
||||||
completer.complete(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
print("[Background $backgroundTask] Not configured");
|
|
||||||
completer.complete(true);
|
|
||||||
}
|
}
|
||||||
return completer.future;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
Reference in New Issue
Block a user