background_locator_fixed icon indicating copy to clipboard operation
background_locator_fixed copied to clipboard

Service stopped suddenly in release mode android

Open m-a-wahab opened this issue 1 year ago • 7 comments

I tested the package in two devices in release mode it stopped suddenly after working for multi hours (8 hours).

m-a-wahab avatar Mar 11 '23 18:03 m-a-wahab

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.

tulioccalazans avatar May 04 '23 14:05 tulioccalazans

Were you able to solve the problem? I am having the same issue, app is terminated automaticaly after some random time.

IKRAMULHAQ786 avatar Aug 02 '23 13:08 IKRAMULHAQ786

Hi, I have two different applications using this library and I don't have any problem.

Send me some code so I can analyze.

tulioccalazans avatar Aug 02 '23 14:08 tulioccalazans

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();
    }
  }

IKRAMULHAQ786 avatar Aug 02 '23 15:08 IKRAMULHAQ786

if you need to see the project please provide your email so that i can add you to my github repo. thanks

IKRAMULHAQ786 avatar Aug 02 '23 15:08 IKRAMULHAQ786

same issue. it stopped after some random time.

Abdulah-butt avatar Aug 09 '23 16:08 Abdulah-butt

it is stopping after 10 hours anybody resolved these issue ?

shashikiran918 avatar Aug 06 '24 05:08 shashikiran918