简体   繁体   English

编程在Android上的一个OnClick功能中关闭电源并打开电源

[英]Programming powering off and powering on in one single OnClick function on android

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. 我想编写一个活动,单击一个按钮后关闭屏幕,然后在2秒钟后将其重新打开。 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. 但这只有在onClick函数返回时才生效。 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. 我需要找到一种方法在函数返回之前强制应用设置,以便稍后可以在同一onClick调用上2秒钟调用开机功能。

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! 预先感谢您的帮助!

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. 将背光设置为0亮度。 You cannot power off the screen programmatically, last I checked. 我上次检查过,您无法以编程方式关闭屏幕电源。

But it would only take effect when then onClick function returns. 但这只有在onClick函数返回时才生效。

Correct. 正确。 That is the way Android works. 这就是Android运作的方式。 The UI is single-threaded. UI是单线程的。 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() . 您更改屏幕亮度的请求已放入队列中,直到您通过从onClick()返回将控制权返回给Android之前,该消息才会得到处理。

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. 我需要找到一种方法在函数返回之前强制应用设置,以便稍后可以在同一onClick调用上2秒钟调用开机功能。

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. 可能最有效的方法是在您的一个小部件上调用postDelayed()

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. WakeLock是您在后台服务中使用的东西,可以在您完成某些工作时使设备保持一会儿的唤醒状态。 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). 它也是内部用于处理android:keepScreenOn这样的android:keepScreenOn (用于电子书阅读器,视频播放器以及用户可能会暂时不点击屏幕的其他活动)。 It has nothing to do with screenBrightness . 它与screenBrightness

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

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