flutter-plugins
flutter-plugins copied to clipboard
problem in pedometer package
hey i'm use pedometer package to step count app if i close the example app and walk it still count steps but in android 10 not count any steps if the app is closed and after i boot the device restart or shutdown the steps don't count again auto till i open the app again and the steps set to 0 and start count again any help ?
I was investigating this myself a few days back. The docs from Google are confusing at best.
In general from this link it seems we will no longer get pedometer events continuously when the app is in background after Android 9.
And then as I posted in this stackoverflow I could not find a way to make it work using the JobScheduler either.
The step count will always reset on reboot so that is expected but even if I register a listener for the pedometer sensor I fail to get the updated stepcount after Android 9 builds (I have only tested on the Pixel 3a where it does not work anymore while on my older Android 7 device it works as before).
any solution for the problem ?
If the app is closed, the step count events should not be fired. This is intentional.
The problem in my case is that I wanted to see approx how many steps the user took per day. So even if I register the pedometer sensor so it does count, I only get the updated count when I open the app so if I have not opened the app for a week, there is no way to tell the daily step count.
I was hoping to be able to use the JobScheduler for this as it is mentioned here: https://developer.android.com/guide/topics/sensors/sensors_motion#sensors-motion-stepcounter
but I was not successful with that approach in devices running Android 9+.
If the Jobscheduler approach would have worked, I could have read the updated step count hourly and that would have been enough but it seems this is not reported in background anymore, not even to the JobScheduler. If someone has better luck it would be interesting to know, otherwise I guess Google Fit is the only option.
@johanlantz at the end i made it with google fit with fit kit package https://medium.com/@harshdeep878/google-fit-with-fit-kit-package-d699fb131034
If the app is closed, the step count events should not be fired. This is intentional.
@thomasnilsson is there a walk around running the pedometer step count event in the background. I tried use it in conjunction with background fetch that allows dart code to be run in background. I would expect the pedometer to be able to get these data in the background. Is there anyway I can get this data in BG, please advise. Thanks
I solved in Android using a foreground sevice with: https://pub.dev/packages/flutter_foreground_task.
The think is by design the stepcounter sensor don't send any data while the application is in background: https://developer.android.com/guide/topics/sensors/sensors_overview#sensors-practices
I solved in Android using a foreground sevice with: https://pub.dev/packages/flutter_foreground_task.
The think is by design the stepcounter sensor don't send any data while the application is in background: https://developer.android.com/guide/topics/sensors/sensors_overview#sensors-practices
@lluisclava will the pedometer work with foreground task if i clear the app from background ? I am able to get steps in background using pedometer and foreground task. but if i clear my app from the app switcher the pedometer disconnects but the foreground tasks still runs.
I solved in Android using a foreground sevice with: https://pub.dev/packages/flutter_foreground_task. The think is by design the stepcounter sensor don't send any data while the application is in background: https://developer.android.com/guide/topics/sensors/sensors_overview#sensors-practices
@lluisclava will the pedometer work with foreground task if i clear the app from background ? I am able to get steps in background using pedometer and foreground task. but if i clear my app from the app switcher the pedometer disconnects but the foreground tasks still runs.
Yes. Until you don't clear the foreground servive it will continue working. You need to connect and register to pedometer in the foreground service in your Taskhandler implementation.