In android, how can i turn off the screen programatically?

折月煮酒 提交于 2019-12-12 08:39:06

问题


I am trying to turn off my screen programatically.

The following methods i have tried:

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

The previous mentioned method does not work.

This however, works:

DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

mDPM.lockNow();

This works, but i am turning my screen off while the phone is ringing, and this causes the ringer to stop ringing. The same as if you are pressing the power button.

Well, i need to be able to turn off my display WITHOUT using DevicePolicyManager method. Or a way to set it that DevicePolicyManager method won't stop my device from ringing.

Thanks all!


回答1:


its better to decrease the screen time out time to turn the screen off . like this :

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 10);

10 means 10 ms you can set your own !



来源:https://stackoverflow.com/questions/18679455/in-android-how-can-i-turn-off-the-screen-programatically

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