2

I am working on a musical training game, which I'm using Unity 3D to create.

It fires out random notes.

Problem is, my android device has only five options for "time before screen goes off", and the longest of these is 10 minutes.

So after 10 minutes the screen goes off, and the app stops running.

  1. How can I keep the app running beyond this 10 minute cut-off?

  2. Is there any way I can override to turn the screen off after one minute (obviously while it continues to emit notes -- this would be a useful power saving feature) or keep the screen on indefinitely

P i
  • 29,020
  • 36
  • 159
  • 267
  • Possible duplicate of [How do I keep the screen on in my App?](http://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app) – Geeky Guy Sep 19 '13 at 21:19
  • Thanks for the link! However, the linked question just talks about keeping the screen on. It doesn't answer how to keep the app running with the screen off. – P i Sep 19 '13 at 21:29
  • 1
    I see. I've retracted my close vote, then. Also +1, because now I'm curious as well. – Geeky Guy Sep 19 '13 at 21:30

2 Answers2

2
Screen.sleepTimeout = (int)SleepTimeout.NeverSleep;
Application.runInBackground = true;

However, the documentation for Screen.sleepTimeout says that you can only set it to one of the two SleepTimeout presets (system default and never sleep).

Also Screen.sleepTimeout used to take a float, now it takes an integer, hence the typecast.

It does all seem bit of a mess, as these links will demonstrate:

http://forum.unity3d.com/threads/201359-How-to-gain-control-over-10-minute-screen-app-timeout?p=1363228

http://forum.unity3d.com/threads/117997-Screen-sleepTimeout-0-on-Android

http://answers.unity3d.com/questions/46204/stop-mobile-screens-turning-off.html

http://answers.unity3d.com/questions/403092/how-do-i-reset-screensleeptimeouts-user-input-time.html

James McMahon
  • 48,506
  • 64
  • 207
  • 283
P i
  • 29,020
  • 36
  • 159
  • 267
  • 1
    NeverSleep is now an int. According to the documentation it can now be set to the number seconds before the screen sleeps. – James McMahon Mar 26 '16 at 19:26
1

When you turn off the screen the application goes into "onPause", then the OS is responsible for freeing memory automatically.

You need an android service