How to keep display active in Firefox OS?

232 Views Asked by At

I want to prevent the Phone display from switching off when my App is open and in the foreground.

Is there an API to do so?

2

There are 2 best solutions below

0
On BEST ANSWER

I asked a similar question a while ago on the dev-webapps mailing list. Frédéric is correct in that you can use the navigator.requestWakeLock('screen') to obtain a lock, however it is a myth that you always need to release the lock.

To qoute one of the replies to my question above:

You do not have permission to hold the lock past the user leaving or closing the app. So the lock will be automatically [released] for you. We'll even re-grab the lock when the user brings the app back from the background so no need to do that either

I have tested this in both hosted apps and packaged app, and that is exactly how it will work. So, if you intend to keep the screenlock for as long as your app is open, then all you have to do is to add this to your app:

navigator.requestWakeLock('screen');

However, if you want to give user a setting option to prevent the app from locking, then you would need to keep the reference to the lock and release it as soon as user changes the setting. Otherewise you are good to go. You can find more information about requestWakeLock on MDN.

2
On

You can use the requestWakeLock function to achieve this by doing

var lock = navigator.requestWakeLock('screen');

However, don't forget to unlock the screen later by doing

lock.unlock();