Stop screen from dimming by enforcing wake lock

2 votes · 1 comment

Enforcing a wakelock is sometimes necessary but should be used with caution as it will take it's toll on the battery.

raw ·
· download
import; import android.content.Context; import android.os.Bundle; import android.os.PowerManager; public class DoNotDimScreen extends Activity { private PowerManager.WakeLock wl; @Override protected void onCreate(Bundle savedInstanceState) { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen"); } @Override protected void onPause() { super.onPause(); wl.release(); } @Override protected void onResume() { super.onResume(); wl.acquire(); } } You will need to include this permission in your manifest: <uses-permission android:name="android.permission.WAKE_LOCK" />
Add a comment

1 Comment

Can I use it for onCreare/onDestroy (wl.release) only?

I try doing this and when onDestroy the program will always crash on the emulator. I also try releasing wl in onStop and the result is same.

PS: I have problem using onResume on my game (it will restart the game rather than resume the game play), so I didn't use onResume.

Reply · July 30, 2009, 8:22 a.m.