Skip to content

Service stops when app is killed #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aeropagz opened this issue Oct 13, 2021 · 7 comments
Closed

Service stops when app is killed #19

aeropagz opened this issue Oct 13, 2021 · 7 comments

Comments

@aeropagz
Copy link

As title says, the service is closed when the app is killed. Is this the right behavior? If so is there a way to prevent this?

@Dev-hwang
Copy link
Owner

The service works normally for actions that move to the background (back button, home button, screen off, remove from the list of recently launched apps, etc). But, service termination cannot be prevented by forcibly stopping the app in the processor manager or application information screen.

@aeropagz
Copy link
Author

In this article they say it is possible. Is this out of date?

@Dev-hwang
Copy link
Owner

This plugin is already implemented in a similar way as in this article.
ForegroundService.kt

I know that forced process killing by the user or the system cannot be prevented. If there is a better way, please let me know.

@aeropagz
Copy link
Author

aeropagz commented Oct 14, 2021

Ah well I see. Thnx for your fast reply. Just one more question, is there a way to limit sensor requests? I tried to fetch only the first element in onStart callback of the stream and thought due to the foregroundTaskOptions it would restart again but it didn't.

Example:

...
foregroundTaskOptions: const ForegroundTaskOptions(
        interval: 1000 * 60 * 15, //every 15th min
        autoRunOnBoot: true,
      ),
....


...
@override
  Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
    final pedometer = Pedometer.stepCountStream;
    final StepCount event = await pedometer.first;
    FlutterForegroundTask.updateService(
        notificationTitle: 'Current steps', notificationText: '${event.steps}');
    sendPort?.send(event);
  }
...

Expected behavior: Every 15th min I get the amount of steps of sensor.

What did I wrong?

@Dev-hwang
Copy link
Owner

The TaskHandler implementation class has 3 functions (onStart, onEvent, onDestroy).

onStart: Called when the task is started. Called only once in a lifecycle.
onEvent: Called when an event occurs. According to the settings above, it will be called every 15 minutes.
onDestroy: Called when the task is destroyed. Called only once in a lifecycle.

So do the above code in onEvent.

  @override
  Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
    final pedometer = Pedometer.stepCountStream;
    final StepCount event = await pedometer.first;
    FlutterForegroundTask.updateService(
        notificationTitle: 'Current steps', notificationText: '${event.steps}');
    sendPort?.send(event);
  }

@aeropagz
Copy link
Author

Ah great! Thnx again and nice lib!

@jairusfdes
Copy link

I am trying something similar, but when i try to access the Pedometer package (https://pub.dev/packages/pedometer) in my task handler it doesn't work. the sensor event listeners are not registered . I don't see any errors in the logs.
If i call the pedometer from my app it works . but not if i put it in the handler . Is there some other setup for the Handler to be able to initate the pedometer package sensor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants