Skip to content
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

bug: startPeriodicTask doesn't always run exactly on the given interval #4

Open
toonvanstrijp opened this issue Jan 6, 2021 · 9 comments
Labels
bug Something isn't working

Comments

@toonvanstrijp
Copy link

I'm using the startPeriodicTask which works well, but it doesn't always run on the given interval.

I'm setting it up as followed:

FlutterForegroundServicePlugin.startPeriodicTask(
  periodicTaskFun: periodicTaskFun,
  period: const Duration(minutes: 15),
);

Then my periodicTaskFun:

void periodicTaskFun() {
  FlutterForegroundServicePlugin.executeTask(() async {
    // initialize garmin
    print("initializing...");
    await GC.GarminCompanion.init(licenseKey);
    print("initialized");

    if (await GC.GarminCompanion.isBluetoothEnabled()) {
      print("getting devices...");
      final foundDevices = await GC.GarminCompanion.devices();
      // wait for device setup to finish when pairing
      print("waiting for first device...");

      if (foundDevices.length == 0) {
        return await FlutterForegroundServicePlugin
            .refreshForegroundServiceContent(
          notificationContent: NotificationContent(
            iconName: 'ic_launcher',
            titleText: 'Wavy',
            bodyText: 'Connect wearable',
            priority: NotificationPriority.high,
            enableVibration: true,
            enableSound: true,
            color: Colors.blue,
          ),
        );
      }

      final device = foundDevices.first;
      print("device: ${device.address}");
      var events = [];
      StreamSubscription liveDataSubscription = device.liveData([
        GC.RealTimeDataType.HEART_RATE_VARIABILITY,
        GC.RealTimeDataType.HEART_RATE
      ]).listen((event) {
        events.add(event);
        print("data: ${events.length}");
        FlutterForegroundServicePlugin.refreshForegroundServiceContent(
          notificationContent: NotificationContent(
            iconName: 'ic_launcher',
            titleText: 'Wavy',
            bodyText: "data: ${events.length}",
            priority: NotificationPriority.high,
            color: Colors.blue,
          ),
        );
      });
      await Future.delayed(const Duration(minutes: 1), () {});
      await liveDataSubscription.cancel();
    } else {
      await FlutterForegroundServicePlugin.refreshForegroundServiceContent(
        notificationContent: NotificationContent(
          iconName: 'ic_launcher',
          titleText: 'Wavy',
          bodyText: 'Enable bluetooth for wavy to work!',
          priority: NotificationPriority.high,
          color: Colors.blue,
        ),
      );
    }
  });
@SayWut
Copy link
Owner

SayWut commented Jan 6, 2021

Is it not running at all or is it running and get stack after the refreshForegroundServiceContent?

I found a bug that the refreshForegroundServiceContent isn't finishing which makes the thread to wait for the function forever
so maybe it because of that but I don't know yet. I will test it and I will let you know

@SayWut
Copy link
Owner

SayWut commented Jan 6, 2021

I think I found the problem, when the OS stops the service the task restart its delay and not continue it from where it stops
so if you use a long interval time and the OS kills the service before the task execute it will never run

I will fix this on the next update

@SayWut SayWut added the bug Something isn't working label Jan 6, 2021
@SayWut
Copy link
Owner

SayWut commented Jan 8, 2021

I uploaded a new update (0.1.7) which should fix this problem
Let me know if it fixed or not

@yakupbaser
Copy link

yakupbaser commented Jan 9, 2021

sorry for not opening new issue but startPeriodikTask is not working when i activate the screen lock. if i close screen lock then startPeriodikTask is going to work well. So is there a way working on screen lock?

@toonvanstrijp
Copy link
Author

It's doesn't always work for me, don't really know what the problem could be. But I think it has to do something with the battery optimization done by android. And when the phone is in doze mode it also doesn't run on the given period.

@SayWut
Copy link
Owner

SayWut commented Jan 9, 2021

sorry for not opening new issue but startPeriodikTask is not working when i activate the screen lock. if i close screen lock then startPeriodikTask is going to work well. So is there a way working on screen lock?

It's doesn't always work for me, don't really know what the problem could be. But I think it has to do something with the battery optimization done by android. And when the phone is in doze mode it also doesn't run on the given period.

Which phone do you testing on?
Maybe it is because doze mode and standby mode but I don't know yet. It is not easy to test this stuff because every phone works differently. I will do my best

@yakupbaser
Copy link

yakupbaser commented Jan 9, 2021

redmi note 4.x.

and one issues too. For example foreground service time duration is 10 seconds. And startPeriodicTask is getting 20 seconds. So foreground service opens a new startPeriodicTask instance. Thts a problem. ForeGround Service must be wait the startPeriodicTask is finished then. Foreground service time duration should be start, i think.

@SayWut
Copy link
Owner

SayWut commented Jan 9, 2021

redmi note 4.x.

and one issues too. For example foreground service time duration is 10 seconds. And startPeriodicTask is getting 20 seconds. So foreground service opens a new startPeriodicTask instance. Thts a problem. ForeGround Service must be wait the startPeriodicTask is finished then. Foreground service time duration should be start, i think.

I fixed this problem in the last update (0.1.7). When the OS stops the service and the timer (task) is running I added a calculation to the new created timer which caclute the delay. For example if the period is 20 min and the service killed after 10 min the delay will be 10 min in the new created timer

@SayWut
Copy link
Owner

SayWut commented Jan 9, 2021

@yakupbaser @toonvanstrijp
I have no idea why your task isn't running
I have tested my example app with galaxy 8 and enabled doze mode and standby mode with period of 20 minutes and it executed every time

Can you try to run the example app and see if the task not executing there too?
Maybe somthing else in your app is breaking it? try to print the current time as the first line of code in the task method and see if it is printing if it is then the task is running as it should

btw use the latest update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants