background_locator_fixed
background_locator_fixed copied to clipboard
Service stopped suddenly in release mode android
I tested the package in two devices in release mode it stopped suddenly after working for multi hours (8 hours).
Perhaps the OS could have killed the application.
Try to run the app in release mode, but connected by a usb cable with Android Studio open to obtain the reason for the crash in logcat.
Were you able to solve the problem? I am having the same issue, app is terminated automaticaly after some random time.
Hi, I have two different applications using this library and I don't have any problem.
Send me some code so I can analyze.
I just copied the code from example project and then placed my logic on callback file. in my scenario the app collects user location after 1 minute and when 10 locations are collected the api is called to send the locations to server.
at first i included the location collection and api call code into the port.listen({}); method but it stops working when the app is terminated.
now i have moved that code inside the callback and it works fine. but after some random hours it stops.
this is the code of callback:
Future<void> callback(LocationDto locationDto) async {
print('${DateTime.now().hour}:${DateTime.now().minute}');
print('$_count location in dart: ${locationDto.toString()}');
await setLogPosition(_count, locationDto);
// await LocationManager.saveLocations(LocationModel(
// latitude: locationDto.latitude, longitude: locationDto.longitude));
final loc = LocationModel(
latitude: locationDto.latitude, longitude: locationDto.longitude);
locations.add(loc);
//TODO: the api calling code should be written here
if (locations.length == 10) {
locationApi();
}
final SendPort? send = IsolateNameServer.lookupPortByName(isolateName);
send?.send(locationDto.toJson());
_count++;
}
the apiCall method is below:
void locationApi() async {
print('Hitting the location api');
try {
// Check if the list of locations has any data
if (locations.isEmpty) {
displayToast('No Location Collected Yet!!', backColor: red);
return;
}
String token = await getUserToken();
Map<String, String> headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token'
};
final uri = Uri.parse('$apiHeader/devicelocation');
// Create a list of location coordinates
List<Map<String, double>> coordinates = locations
.map((location) => {
'latitude': location.latitude,
'longitude': location.longitude,
})
.toList();
final json = jsonEncode(coordinates);
print(json.toString());
final result =
await http.Client().post(uri, body: json, headers: headers);
if (result.statusCode != 200) {
print('${result.statusCode}: ${result.reasonPhrase}');
} else {
//clear the location list
locations.clear();
//apiCounter++;
Map<String, dynamic> jsonData =
jsonDecode(result.body) as Map<String, dynamic>;
CommonModel model = CommonModel.fromJson(jsonData);
if (model.success) {
print('device location sent');
//displayToast(model.message);
} else {
print('Error: device location not sent');
displayToast(model.message);
}
}
} on Exception catch (e) {
print('Exception: ${e.toString()}');
} finally {
// locations.clear();
}
}
if you need to see the project please provide your email so that i can add you to my github repo. thanks
same issue. it stopped after some random time.
it is stopping after 10 hours anybody resolved these issue ?