I have both the Run In Background field of Player settings checked, and I am setting Application.runInBackground to true in code, and when I minimize the game by pressing the Home button, it does not continue running in the background.
I remember this working at one point, but I can’t remember when it stopped working for me.
I’m running this on a HTC Droid Incredible, if that matters.
Am I missing something on how this is supposed to work?
Application.runInBackground is only for Webplayer (and PC, not sure about the last one though)
Unity runs as Activity in Android, hence it gets paused when it’s loses it’s focus. That’s by (Android) Design and works as intended.
The only way to run stuff in background on Android is through a Service. A service however, can’t have any visible parts, hence running a game or even unity as a service is impossible.
I assume you have a simulation game, like Sim City etc. where player builds a city or whatever, and the comes back next day to get his income etc, am I right?
If so, you should use OnApplicationPause method to get notified when the game pauses/unpauses.
...
public void OnApplicationPause(bool paused) {
if(paused) {
// Game is paused, remember the time
} else {
// Game is unpaused, calculate the time passed since the game was paused and use this time to calculate build times of your buildings or how much money the player has gained in the meantime.
}
}
...
There is no other way… gladly. Otherwise users would be pissed if your game eats up their battery life and their phone runs out of power after 2-3 hours, because your game wastes their battery and CPU etc. (not to mention that their phone would run warm if you keep doing stuff like a game in background)
I’m running into this problem now with a GPS app I am writing. Whenever the user goes away to a different screen the GPS stops. When they come back, it’s reinitialized and they amazingly jump miles depending on how long they’ve been away from the main screen for - any gps data in that time is lost of course, so no analysis of elevation, speed or anything. The same situation occurs if they turn the screen off.
The game I am building would really like to read GPS data in the background while there is no action going on. Running it as a service is the only way that makes sense. Are there any options?
…and the player’s battery and the heat of the phone should be up to the player; have you ever run the Maps/Navigation on a long trip on a Droid? It will die after 45 minutes or burn through your floorboards if you have it plugged in.
there is no reasonable way without wasting battery cause you can never know when the game only went into background and when it really meant to stop and die.
Otherwise, the solution is definitely to use a service, as your application is in for being cleaned up very fast anyway
Thanks for the reply Dreamora. Are you are saying that it is not possible to run a Unity game in the background, or, that there is actually a way and it will really waste your battery?
In all my experiments, the game pauses when it is in the background and all I would like it to do is to keep reading GPS (like a service).
it works sometimes.
it’s supposed to work in android now too. but doesn’t always. (some mobile vr like gear depends on it). the setting is supposed to work.
And strictly speaking, “The only way to run stuff in background on Android is through a Service. A service however, can’t have any visible parts, hence running a game or even unity as a service is impossible.” is not true at all.
you can run stuff in background simply by creating a java thread(which creates a linux thread…). the “service” normally is just that, except it hooks up to the android api to tell that it’s running and can receive messages and would rather not get shut down by the OS. it will run until the os kills the app though if you just leave a thread running in there - you could leave it running even if you remove the activity(and still don’t have a service).
and you can have an UI without having an activity, like the fb floaters. don’t always assume the google beginner “this is how it must be” beginner docs are true, because they’re not.
of course it will use battery, if you consider it a waste is another thing though of course, but there are times you would want it to happen.
all you need to do is edit out the calling of unity players paused method from the activity your unity app is running in.
it will continue to run as long as the activity isn’t killed.
easiest manual method is to export the project from unity (choose “google android project” when building and the export button appears). after that you have to import to some ide or build from the command line, but before that go edit the unityactivity .java inside the src folder. comment out the calling of the pause.
(what tseng said, while technically being what google tells you, isn’t strictly how android and dalvik/adt lifecycles work).
if you want to make it less likely that android will kill your app you can connect a service from your android process or keep something on display all the time(like an invisible floater, whatever), post an ongoing notification or like that. what process will get shut down first on android depends on a lot of factors like that, including supposedly memory use. being displayed or being a content provider in use etc all can bump up your process priority to make it less likely to get shut down on low resources.
it would be far simpler if the runinbackground just worked like it’s supposed to. it does when in gearvr mode so i guess there is some override in that.
Thanks for the suggestion. This works great. Just want to add that in my case it was UnityPlayerNativeActivity’s on Pause that I had to hack rather than
UnityPlayerActivity
Why is that? What should we use instead of UnityPlayerNativeActivity? (found UnityPlayerActivity) Some AndroidManifest.xml included with plugins uses it. If I change UnityPlayerNativeActivity to UnityPlayerActivity, will the plugin still run?
@glassfin I tried this and built with Android Studio but every time I install the apk on my phone the app crashes as soon as it is opened. Although the Unity project build never crashes. Any ideas as to what I could be doing wrong in Android Studio? How do you build projects outside of Unity? Thanks
It looks like you know how to do. I can’t find the src folder. I don’t know if it’s normal or not. Could you or someone else please help me?
I am using Unity 5.5 (I don’t know if it can help)
Has application run in background been fixed yet? Does it work for both android and iOS? If not how does google maps minimise their app when navigation is running. This is vital for my projects success.