在 Android 上通过一个 OnClick 函数编程关闭和打开电源
我想编写一个活动,单击按钮后关闭屏幕,然后在 2 秒后将其重新打开。 我尝试使用以下代码来关闭屏幕:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0/(float)255;
getWindow().setAttributes(lp);
但只有当 onClick 函数返回时它才会生效。我尝试将其运行到处理程序中,但没有成功。 我需要找到一种方法来强制在函数返回之前应用设置,以便我可以在 2 秒后在同一个 onClick 调用中调用开机函数。
我还发现之后唤醒设备非常困难。 虽然如果我使用物理按钮关闭屏幕电源,此代码可以工作,但是当使用前面描述的技术关闭手机电源时,该代码似乎不起作用。
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE ,"Dev Tag");
try
{
wl.acquire();
wl.release();
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(),20).show();
}
预先感谢您的帮助!
I would like to write an activity that after clicking on a button turns off the screen and then turns it back on after 2 secs.
I tried using the following code in order to power off the screen:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0/(float)255;
getWindow().setAttributes(lp);
But it would only take effect when then onClick function returns. I tried running it into a handler but with no success.
I need to find a way to force the setting to get applied before the function returns so that I can call the power on function 2 secs later on the same onClick call.
I also found it very hard to wakeup the device afterwards.
While this code works if I power off the screen using the physical button it doesn't seem to work when the phone is powered off using the technique described previously.
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE ,"Dev Tag");
try
{
wl.acquire();
wl.release();
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(),20).show();
}
Thanks you in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但不会关闭屏幕电源。这会将背光亮度设置为 0。我上次检查过,您无法以编程方式关闭屏幕。
正确的。这就是 Android 的工作方式。 UI 是单线程的。您更改屏幕亮度的请求将被放入队列中,并且在您通过从
onClick()
返回将控制权返回给 Android 之前,该消息不会被处理。这是不可能的。也没有必要。两秒过去后,有很多方法可以控制。最有效的方法可能是在您的某个小部件上调用
postDelayed()
。设备一开始就没有休眠。
WakeLock
是您在后台服务中使用的东西,可在您完成某些工作时使设备保持唤醒状态一段时间。它还在内部用于处理像android:keepScreenOn
这样的想法(用于电子书阅读器、视频播放器以及用户可能暂时不会点击屏幕的其他活动)。它与屏幕亮度无关。That does not power off the screen. That sets the backlight to 0 brightness. You cannot power off the screen programmatically, last I checked.
Correct. That is the way Android works. The UI is single-threaded. Your request to change the screen brightness is put on a queue, and that message will not be processed until you return control to Android by returning from
onClick()
.This is not possible. It is also not necessary. There are plenty of ways to get control after two seconds has elapsed. Probably the most efficient is to call
postDelayed()
on one of your widgets.The device was not asleep in the first place.
A
WakeLock
is something you use in a background service to keep the device awake for a few moments while you complete some work. It is also something used internally to handle thinks likeandroid:keepScreenOn
(used for ebook readers, video players, and other activities where the user might not tap the screen for a while). It has nothing to do withscreenBrightness
.