0
\$\begingroup\$

I am trying to develop a single demo in unity using c#, where I've put counter for minutes and seconds. When the game was in background, still it should keep counting minutes and seconds. After research on google, I've found a function to keep running application in background.

Application.runInBackground=true;

This works exactly when application exist in stack. As soon as it is removed from application stack, it stops every scripts. What should i implement to keep running application all time in background, even when it is not in application stack.

\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

I don't think you'll be able to do this. For performance reasons and to save battery Android will stop every app that is not active and it happens by design. Even if you managed to find a way to do this, your app would drain battery as if it were active so nobody would use it for long :).

If all you need is to do some action after few minutes you can use Android timers to do that - the same way as mail clients check for incoming messages. If you want to get the time between runs of your application you can store current time on exit and compute the delta on the next run, and so on - for most of you needs there is probably some Android-specific way to do this.

BTW: Unity is a generic engine not designed for any specific platform but it needs to work under limits of any platform you target. Because of this not every functionality will always work as you expect - that's the cause of your runInBackground issue. Looking for a way to solve such issue don't start from Unity but look at Android SDK to find some Android specific solution, when you have one then you can check how you could do this in Unity.

\$\endgroup\$
1
  • \$\begingroup\$ "I don't think you'll be able to do this." - why not? applications do this. I have a setting on my phone to do this. "your app would drain battery as if it were active so nobody would use it for long" - battery drain depends on what the app is doing, while it is in the background. So again, it depends how its set up. YouTube red comes to mind, an example where a major company (the developers of the actual hardware) charges people for use of an application in the background. \$\endgroup\$
    – Gnemlock
    Commented Sep 14, 2016 at 0:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .