tags:

views:

273

answers:

3

Hi

Could someone show me in a snipet of code how to set the backlight always on in android ?

+1  A: 
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK , "My Tag");
wl.acquire();
 // ..screen will stay on during this section..
wl.release();

Nb. the keyboard backlight will be allowed to go off.

Schildmeijer
what happens if the I exit the application before I release the lock ?
rantravee
Also , are there allowed multiple locks being acquired at the same time ?
rantravee
You need to release the lock before your application exits, it'll complain otherwise. Multiple locks can be acquired at the same time, and then all need to be released for the backlight to return to normal behaviour.
bdls
As hackbod answered below, and was bizarrely voted down, this question has already been answered more simply and with an explicit plea not to use wake locks.
Christopher
+2  A: 

As an alternative to WakeLock, I'd suggest using the FLAG_KEEP_SCREEN_ON flag.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

This is easier to use than WakeLock, as you don't need to worry about releasing it when the activity is paused/destroyed.

Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright.

bdls
This is definite the way to go. The wake lock is to different to manage and to be sure that it is destroyed all the time.
Janusz
A: 

Already asked and answered here:

http://stackoverflow.com/questions/2131948/force-screen-on

hackbod