Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Application.runInBackground is not working on Android

Discussion in 'Android' started by Sicarius43696, Jan 2, 2012.

  1. Sicarius43696

    Sicarius43696

    Joined:
    Jan 2, 2012
    Posts:
    1
    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?
     
  2. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Of course it doesn't work.

    1. Application.runInBackground is only for Webplayer (and PC, not sure about the last one though)
    2. 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.

    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnApplicationPause.html

    Code (csharp):
    1.  
    2. ...
    3.  
    4. public void OnApplicationPause(bool paused) {
    5.     if(paused) {
    6.        // Game is paused, remember the time
    7.     } else {
    8.        // 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.
    9.     }
    10. }
    11. ...
    12.  
    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)
     
  3. fanjules

    fanjules

    Joined:
    Nov 9, 2011
    Posts:
    167
    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. :(

    What are my options?
     
    Last edited: Jul 24, 2012
  4. grenpop

    grenpop

    Joined:
    Nov 5, 2012
    Posts:
    2
    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.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    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
     
  6. grenpop

    grenpop

    Joined:
    Nov 5, 2012
    Posts:
    2
    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).

    thanks for any help!
     
  7. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    The service would be written outside of Unity, basically as a plugin, I would imagine.
     
  8. glassfin

    glassfin

    Joined:
    Jul 25, 2016
    Posts:
    2
    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.
     
  9. glassfin

    glassfin

    Joined:
    Jul 25, 2016
    Posts:
    2
    Actually I found the nasty easy way.

    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.
     
  10. ayogan2

    ayogan2

    Joined:
    Aug 30, 2016
    Posts:
    1
    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
     
  11. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    806
    Please don't use UnityPlayerNativeActivity if you are on Unity 5.3+.
     
  12. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    806
    Application.runInBackground is not working on Android. I've pushed documentation update to reflect it.
     
  13. boubouk50

    boubouk50

    Joined:
    Jul 10, 2014
    Posts:
    12
    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?

    Thanks.
     
    Last edited: Nov 21, 2016
  14. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    806
    @boubouk50 that's probably a question for the plugin author :)
    Yes use UnityPlayerActivity instead of UnityPlayerNativeActivity.
     
  15. NesCroft

    NesCroft

    Joined:
    Apr 10, 2015
    Posts:
    2
    @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
     
  16. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    228
    is possble to keep the radio stream playing?
     
  17. Nizy

    Nizy

    Joined:
    Jan 22, 2015
    Posts:
    1
    Hello,

    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)

    Thank you for the answer
     
  18. Kraato5

    Kraato5

    Joined:
    Mar 11, 2015
    Posts:
    2
    Hey, mate.
    I'm pretty sure they just switched it with "Source" folder.
    Unexpected, right? :)
     
  19. Krunal_vasundhara

    Krunal_vasundhara

    Joined:
    Apr 27, 2017
    Posts:
    35
    Hello sir , how can i call update method continuously in unity while the game is in background? beacuse with your solution it wont work.
     
  20. levi9687

    levi9687

    Joined:
    Dec 17, 2017
    Posts:
    4
    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.
     
  21. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    806
  22. unity02_orbit

    unity02_orbit

    Joined:
    Jun 1, 2018
    Posts:
    1
    Hello
    Is there examples on how to connect Android background service and Unity? All I want is to have delayed calculation and ability to send short data via network. How can this be made possible on mobile? And since Application.runInBackground are not available for mobile, is it possible to have this functionality at some point. I fully understand battery saving concerns, but it would be nice to have slow updates, like co-routine, and warn developers to not "abuse" it.

    And, I know it is Android thread, I have same issue on iOs too (despite "Application does not run in background" = "NO" and "Required background modes" = Required background modes, App downloads content from the network, App downloads content in response to push notifications)

    Please help