如何把屏幕关闭或发送设备入睡屏幕、设备

2023-09-06 01:55:15 作者:伱走了心空了

我要发送设备睡眠或关闭屏幕。我已经调查并发现这个话题:关闭屏幕在Android

I want to send device to sleep or turn the screen off. I have investigated and found this topic: Turn off screen on Android

基本上有三种方法可以做到这一点,但我发现了三个问题:

Basically, the are three ways to do it, but I have found problems for the three:

一)选择1:

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

问题:这导致FC。我已阅读,我需要 DEVICE_POWER 的权限,但不能被授予正常的应用程序。

Problem: It causes FC. I have read I need DEVICE_POWER permissions, but it can't be granted for normal apps.

二)选择2:

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

问题:这不是为我工作。我不知道为什么。它没有给我一个FC,但它是无害的。

Problem: This does not work for me. I don't know why. It does not give me a FC, but it is innocuous.

三)选择3:

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

问题:我可以得到它的工作,但是当试图把设备它不奇怪的事情,就像不喜欢返回,或者如果我的应用程序是在前面,会自动进入休眠状态刚过按钮pressing。这看起来更像是一个尖端或解决方法比一般的解决方案。

Problem: I can get it to work but when try to turn on device it does strange things, like don't like to return, or if my apps is in front, automatically goes to sleep just after pressing on button. This looks more like a tip or workaround than normal solution.

谁能告诉我送设备睡眠或关闭屏幕关闭,可以运行没有问题什么好的方法?这听起来罕见,我认为这样一个简单的功能还没有使用它的好办法(或免得有据可查)

Can anyone tell me any good method to send device to sleep or turn screen off that can run without problems? It sound rare to me that a simple functionality like this has not a good way to use it (or at lest well documented)

推荐答案

我回答自己。它可以关闭设备,但应用程序必须实现通过API设备管理员功能:的设备管理

I answer myself. It is possible to turn off the device, but the application has to implement the device administrator feature via API: Device Administration

在此API有一个 locknow()函数,做我想做的。

In this API there is a locknow() function that do what I want.

无论如何,这对于扩展Android的硬件和安全功能,这是非常不可能的,任何人想要控制设备的方式。

Anyway, this is for extending the android hardware and security features and it is very unlikely that anyone wants to control the device in that way.

我想是从一个简单的手势锁定装置的能力。由于我不想使自己的设备管理器类我检查,如果免费的应用程序锁屏已安装并启动它。这是一个快捷方式,一个简单的方法做我想做的。这个应用程序必须注册为设备管理器,以便它已经实现了这个类。就有可能得到它从市场自由

What I wanted is the ability to lock the device from a simple gesture. As I dont want to make my own device manager class I check if the free application "lockScreen" is installed and launch it. It is a shortcut and an easy way to do what I want. This app must be register as device manager so it has implemented this class. It is possible to get it free from the market.