2

Possible Duplicate:
Overriding the power button in Android

Is there any possible way I can stop the screen being locked when the power button is pressed.

Community
  • 1
  • 1
Bob
  • 881
  • 2
  • 10
  • 16

3 Answers3

3

You can't stop screen to get lock(short click), But you can get the event of long keypress of Power button

 @Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        //dostuff
        return true;
    }

    return super.dispatchKeyEvent(event);
}
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • FORTUNATELY you can't stop screen from locking. An app should never affect the basic functionality of a device. If an app did that to me it gets uninstalled. – skyfoot Mar 27 '12 at 11:08
-1

try this as per your Requirement,

KeyguardManager keyguardManager;
    KeyguardLock lock;


    public void Disable_AutoLock() 
    {   

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
        lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);        
        lock.disableKeyguard();

    }

    public void Enable_AutoLock() 
    {
        lock.reenableKeyguard();

    }

Give permission in android manifest.

 <uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 
Hasmukh
  • 4,632
  • 2
  • 30
  • 44