Android Java : Turn screen Off

泪湿孤枕 提交于 2020-01-01 05:39:08

问题


I am making an application that turn the screen ON and OFF with the proximity sensor. The proximity code is finished, but i got trouble using the screen controls.

I have read that I should use,

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

For that, I need to grant special permissions in order to make it works, but I haven't figured out how to do it.

Also, I have 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 way only turn the screen off on my application; it doesn't work if my application is running in background.

I have also read about using Wakelock (i use them to wake my phone from screen-off), 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 that, nothing happens.

Is there any other way to do it ?


回答1:


You need to give your App the right permissions to do that:

Add <uses-permission android:name="android.permission.DEVICE_POWER" /> to your Manifest inside the <manifest> Tag

If you want to keep your Screen on use this, as suggested by Dianne Hackborn on Google Plus: KeepScreenOn



来源:https://stackoverflow.com/questions/9294917/android-java-turn-screen-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!