I am making a game which relies on a step counting mechanic for Android. I have step tracking working just fine while the app is running or in the background (using UnityEngine.InputSystem.StepCounter), but it cannot track steps when the app is closed - this is because Unity is a foreground service and therefore can’t actually run anything when closed by itself.
The solution seems to be to create a plugin in Android Studio using Java, which is able to listen for steps in the background. However, I haven’t been able to get this running at all. Looking through the most recent threads on this topic, answers seem to range from “The info on this topic is obscure” to trying the project below or just repeating to make a plugin in Java from scratch (which I have no experience with unfortunately).
Using the git repo below doesn’t seem to work at all. I brought the project up to 2022 to see if it would fit my purpose, immediately crashed on starting the service. Found a proposed change to work with a more recent version of Android API, didn’t crash but still didn’t count steps ever. GitHub - nintendaii/unity-background-service: A simple project that shows how to create an Android service for Unity application working on background
Does anyone have suggestions for what to do from here or any useful information on this topic? Thanks
Android always pauses an app that is minimised, or when the screen is off. There is no way to force it to run like that directly without running the code in a notification. However; I have a workaround that doesn’t require any of this messing about. Just follow the steps in there.
Thank you for the response! I already have the step counting working in the foreground and minimised just to be clear - it works fine using the activity permission like you suggested.
The issue is the “annoying notification” (service) part, which I haven’t been able to get working. My question is how to make a service that actually counts in the background since a recent Android API seems to have broken the old tutorials (such as the one I have linked above) which means I need to go with the service approach. I haven’t been able to figure out how to make the service to run while closed. Do you have any experience with that?
1 Like
You honestly don’t need a notification or anything else if you follow my method. It just updates based on the steps you took while it was offline, so the user never notices. If you follow those steps, and copy the code across, it just works.
1 Like
I’ve been testing this on and off myself and with a few friends using your method which I was already on.
To my understanding, it definitely doesn’t count steps as soon as the app is killed. The issue is that different phone manufacturers are prone to killing the app more quickly than others. For some users the app just dies like 20 steps in and their walks don’t get counted. I’ve been checking the debug that’s just giving me [ StepCounter.current.stepCounter.ReadValue(); ] and I’ve been getting nearly no steps for like a while even though I’ve been walking daily and try to open it before I start walking.
Can you please clarify if I’m misunderstanding your code? I have followed your instructions and even used a blank project just to double check my implementation, still running into issues unfortunately. Would really appreciate any extra detail on this, thanks!
That’s very odd. It should be working, as it does on mine. Did you make sure to get permission in Android Manifest? That is usually what causes the problem. If you don’t get a popup asking for permission, it’s a permissions thing. Is the Debug text field saying anything?
To be clear: this method doesn’t directly count steps when the app is killed. Instead it works out the total steps taken since the last time you activated the app. It can also take a little while to update the readout, but that is unfortunately Unity not my code.
Thanks for the clarification!
Yes, it definitely has my physical activity permission. I checked the settings of the phone itself and the app is definitely allowed to see physical activity. I’m nearly certain it was working as well since I did have a lot of steps logged but recently it doesn’t seem to play ball. I had an Android system update around the same time as this issue rose and I know more recent Android versions have Health Connect built-in whereas I think it used to use Google Fit API. It could be deprecated to some extent, but the steps are still counting so I have no idea at this point.
Not sure I’m clear on the problem. Did you say it’s counting the steps?