简体   繁体   English

android以编程方式关闭屏幕

[英]Turn off screen programmatically android

I am trying to build an app that would launch a service that will turn off the screen when a criteria is met, but it wouldn't lock the phone. 我正在尝试构建一个应用程序,该应用程序将启动一项服务,该服务将在满足条件时关闭屏幕,但不会锁定手机。

I don't want to use PowerManager class because the reference says "Device battery life will be significantly affected by the use of this API." 我不想使用PowerManager类,因为参考文献指出: “使用此API将大大影响设备的电池寿命。” and the main objective of the app is save battery. 该应用程序的主要目的是节省电池。

Is there any other way to turn off the screen (not just set the brightness to minimum) and keep the phone running all the applications? 还有其他方法可以关闭屏幕(不仅仅是将亮度设置为最低),并使手机运行所有应用程序吗?

Hope this helps ! 希望这可以帮助 !

Minimize the screen timeout time. 最小化屏幕超时时间。

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

You can turn off screen by overriding system screen turn off time. 您可以通过覆盖系统屏幕关闭时间来关闭屏幕。

First get system turn off time 首先获取系统关闭时间

int defaultTurnOffTime = Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, 60000);

then put your turn off time Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, 1000); 然后将关闭时间Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, 1000);

and when the screen turned off, set default turn off time Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, defaultTurnOffTime); 当屏幕关闭时, set default turn off time Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, defaultTurnOffTime);

add below permission 添加以下权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM