1

I've made a simple application to turn the screen on and off once a button is pressed, the code does not present any error but it doesn't do anything. Here is the code:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {

        @SuppressLint("Wakelock")
        @Override
        public void onClick(View arg0) {

            Log.d(TAG,  String.valueOf(screenWakeLock));

        if (screenWakeLock != null) {

                   if(screenWakeLock.isHeld())
                        Log.d(TAG, "wavelock held");
                      screenWakeLock.release();
                   screenWakeLock = null;

        }else{

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,   "screenWakeLock");
            screenWakeLock.acquire();
            Log.d(TAG,  String.valueOf(screenWakeLock));

                }

        }

    });


}

and the permissions in the manifest file:

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

There's no error, but nothing happens on the screen. Does anyone know why?

Thank you in advance!

user2115240
  • 29
  • 2
  • 11
  • Do you want to lock the device? – Triode Apr 03 '13 at 11:15
  • Possibly this will help http://stackoverflow.com/questions/9561320/android-how-to-turn-screen-on-and-off-programmatically – Triode Apr 03 '13 at 11:17
  • 1
    what is the use of doing this as its already available for all of the devices through power button!! – Daud Arfin Apr 03 '13 at 11:20
  • Rajesh CP thank you. I tried the code the user says it worked for him: params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON; params.screenBrightness = 0; getWindow().setAttributes(params); but actually for me it does nothing. then I read this in one of the comments: "Does not turn the screen off in the traditional sense. It makes the screen as dim as possible. " and it makes sense. Unfortunately, it seems the user was not able to make it work either. – user2115240 Apr 03 '13 at 14:53
  • David Arfin my whole point was to able to do it without using the power button. – user2115240 Apr 03 '13 at 14:54

1 Answers1

-1

In another question:

Programmatically switching off Android phone

I found a way that works. After rooting you phone:

try {
      Process proc = Runtime.getRuntime()
                .exec(new String[]{ "su", "-c", "reboot -p" });
      proc.waitFor();
} catch (Exception ex) {
      ex.printStackTrace();
}
Community
  • 1
  • 1
user2115240
  • 29
  • 2
  • 11