CoderzHeaven

March 4, 2013

Different ways to KEEP SCREEN ON in Android.

These are the different ways in which you can keep your screen on in your app

1. Declare the screen stays on in your XML layout
2. Inform the window manager in onCreate you want the screen to stay on
3. WakeLock – used for critical downloads or things that you definitely don’t want the Android system shutting down for

First Method

import android.app.Activity;import android.os.Bundle;import android.view.WindowManager;public class ScreenOnFlagActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_flag);getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}}

Next Method

import android.app.Activity;import android.os.Bundle;import android.os.PowerManager;import android.os.PowerManager.WakeLock;public class ScreenOnWakeLockActivity extends Activity {private static final String TAG = "com.blundell.tut.ui.phone.ScreenOnWakeLockActivity.WAKE_LOCK_TAG";private WakeLock wakeLock;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_wake_lock);PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG);}@Overrideprotected void onResume() {super.onResume();wakeLock.acquire();}@Overrideprotected void onPause() {super.onPause();wakeLock.release();}}

The third method

Add this to your root layout

‘android:keepScreenOn=”true”‘

But for all these to work.
You have to add this permission in the AndroidManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />

Note : Don’t use this screen on feature unless you really need it, because it kills your battery.

Website PinFacebook TwitterMyspaceFriendfeedTechnoratidel.icio.usDiggGoogleStumbleUponPremium Responsive

Leave a Reply Cancel reply