简体   繁体   English

4.2中的Android Wakelocks可用于打开和关闭屏幕

[英]Android Wakelocks in 4.2 for flashing the screen on and off

In versions of android before 4.2 I used to have some code to flash the screen on when a notification came into the app (if users wanted it to flash). 在4.2之前的android版本中,当应用程序收到通知时,我曾经有一些代码可以在屏幕上闪烁(如果用户希望它闪烁)。 I used: 我用了:

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
                     PowerManager.SCREEN_DIM_WAKE_LOCK | 
                     PowerManager.ACQUIRE_CAUSES_WAKEUP, "example_tag"
                                         );
wl.acquire();

//this would switch the screen on
//then after a little while I'd call
wl.release(); 

So the "aquire" method would switch the screen on (if it wasn't already) and the "release" would switch it straight back off. 因此,“ aquire”方法将打开屏幕(如果尚未打开),而“ release”将直接关闭屏幕。

In android 4.2 it seems that the switching off doesn't happen when you release the wakelock, but only when the screen timeout setting for the display is reached (default seems to be 2 minutes) 在android 4.2中,当您释放唤醒锁时似乎不会关闭,而是仅在达到显示器的屏幕超时设置时(默认似乎是2分钟)

Is there any way to make it switch off when the wakelock is released. 有什么办法可以使它在唤醒锁释放时关闭。 I've seen some bits in the API about specifically switching the screen on and off, but I'm a little unsure about using as I wouldn't want the app switching the screen off if somebody was in the middle of doing something, so the wakelock seemed to work well. 我已经在API中看到了一些有关专门打开和关闭屏幕的信息,但是我不太确定要使用什么,因为如果有人在做某件事,我不希望应用程序关闭屏幕,所以唤醒锁似乎运行良好。 Any suggestions? 有什么建议么?

The only way I've found it to set the screen timeout down to a short time but store off the default and restore it when finished. 我发现它的唯一方法是将屏幕超时时间设置得很短,但要保留默认值并在完成后将其恢复。 This will turn it down to around 5-7 seconds at the shortest and you have to be careful to restore the setting back correctly afterwards as it's a total hack really. 这将把它缩短到最短的5-7秒左右,您必须要小心,然后再正确地恢复设置,因为这确实是一个大问题。

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

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