在 Android 上通过一个 OnClick 函数编程关闭和打开电源

发布于 2024-12-17 16:20:19 字数 798 浏览 2 评论 0原文

我想编写一个活动,单击按钮后关闭屏幕,然后在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-12-24 16:20:19

我尝试使用以下代码来关闭屏幕

但不会关闭屏幕电源。这会将背光亮度设置为 0。我上次检查过,您无法以编程方式关闭屏幕。

但只有当onClick函数返回时才会生效。

正确的。这就是 Android 的工作方式。 UI 是单线程的。您更改屏幕亮度的请求将被放入队列中,并且在您通过从 onClick() 返回将控制权返回给 Android 之前,该消息不会被处理。

我需要找到一种方法来强制在函数返回之前应用设置,以便我可以在 2 秒后在同一个 onClick 调用中调用开机函数。

这是不可能的。也没有必要。两秒过去后,有很多方法可以控制。最有效的方法可能是在您的某个小部件上调用 postDelayed()

我还发现之后唤醒设备非常困难

设备一开始就没有休眠。

虽然如果我使用物理按钮关闭屏幕电源,此代码可以工作,但是当使用前面描述的技术关闭手机电源时,此代码似乎不起作用。

WakeLock 是您在后台服务中使用的东西,可在您完成某些工作时使设备保持唤醒状态一段时间。它还在内部用于处理像 android:keepScreenOn 这样的想法(用于电子书阅读器、视频播放器以及用户可能暂时不会点击屏幕的其他活动)。它与屏幕亮度无关。

I tried using the following code in order to power off the screen

That does not power off the screen. That sets the backlight to 0 brightness. You cannot power off the screen programmatically, last I checked.

But it would only take effect when then onClick function returns.

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().

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.

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.

I also found it very hard to wakeup the device afterwards

The device was not asleep in the first place.

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.

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 like android: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 with screenBrightness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文