Android Java: turn off the screen

I am making an application that turns the screen on and off using a proximity sensor. The proximity code is complete, but I'm having trouble using the screen controls.

I read that I have to use,

PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
manager.goToSleep(int amountOfTime);

To do this, I need to provide special permissions to make it work, but I did not understand how to do this.

In addition, I read about changing screen brightness

WindowManager.LayoutParams params = getWindow().getAttributes();
            params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
            params.screenBrightness = 0;
            getWindow().setAttributes(params);

But this method only turns off the screen in my application; it does not work if my application is running in the background.

I also read about using Wakelock (I use them to wake my phone from the screen), but when

PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();

But when I do this, nothing happens.

Is there any other way to do this?

+5
source share
1 answer

:

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

, , Dianne Hackborn Google Plus: KeepScreenOn

+1

All Articles