So I have been building an Android app that counts steps and time, but it stops working when the screen is off. I’ve set Application.runInBackground to true… but this wasn’t enough. Any ideas how to keep it going when the screen is off? The counter runs on a coroutine with WaitForSecondsRealtime.
Mobile platforms do not let applications run in the background for battery life purposes (aside from specific purposes, like audio playing). All you can do is simulate the time that has passed the next time the app continues. You may have noticed that Time.time does not increment while the app is in the background, so you’ll have to access the system clock.
You can access this with System.DateTime.Now. I’d advise using the tick count (…Now.Ticks), as it makes the math pretty straightforward (there are 100,000 ticks per second). So you basically: get the current tick count; store it; subtract it from the last tick count stored; convert the difference into a float and divide by 100000. That should give you the number of seconds, in real world time, that has passed since the last time this function had run.
I once considered trying to make an app that could continue to run in the background but I never got around to it, partly because it seems very difficult to do because smartphone manufacturers don’t like things running in the background and draining the battery life.
I believe Android has some sort of background service that an app can subscribe to but some smartphone manufacturers will still kill your app when it’s not focused. Or some manufacturers will insist that the user gives your application permission to run in the background.
To sum up: It’s very messy and unreliable. And finding info on how to do it is very vague.
You simply can’t, not Unity itself. Unity is a foreground activity and android can only have one active at a time. When you switch to a different activity Unity will be paused. Things that run in the background have to be done through native background services. You can not use any of Unity’s methods there but this has to be build in native java . There are many different ways to implement background services. Here seems to be an example.
Okay thank you, that’s a relief after reading all of that other stuff! Is the a solation for iPhones? I downloaded it, but I’m getting a lot of errors.
Unfortunately I just can’t get that working. Is there one that might work on both Android and Iphone?
Not really. How background services work is completely native to the platform. You may find some kind of wrapper that can somewhat “proxy” the native interface, but that’s certainly not that easy. Android uses Java, iOS uses ObjectiveC natively. Background services written in C# would mean you might need an additional AOT mono environment that can run your common C# code. That’s why it’s much easier and smaller to program those things in the respective native environments. Most packages that have crossplatform support provide separate libraries for each platform.
As it was said by others, this is not an easy topic and something that can’t be easily ported across different platforms. The hardware and OS works differently.
Damn… This is a little frustrating as I thought Unity had full mobile functionality. I may have to spend money getting a third party system to make this work. I’ve already waste a hundred hours or so on this app, and I want it to work.