简体   繁体   English

如何检测屏幕何时开启?

[英]How to detect when the screen is on?

As mentioned in a previous question, I am having difficulty intercepting all android.intent.action.SCREEN_ON events without a long-lived Service (discouraged). 如前一个问题所述,我很难在没有长期Service情况下拦截所有android.intent.action.SCREEN_ON事件(不鼓励)。

I may be able to work around the need if I can simply work out when the screen is on at any given time, in the Service . 如果我可以在Service中的任何给定时间点亮屏幕,我可以解决这个问题。

Can anyone suggesting a method call that would return this information? 任何人都可以建议一个方法调用,将返回此信息? 1.5 upwards. 1.5以上。

PowerManager.isScreenOn()自:API级别7

系统广播ACTION_USER_PRESENT意图可以提供任何帮助吗?

If your code is running, there's a very good chance the screen is on. 如果您的代码正在运行,屏幕很可能会启动。 The main exception would be if your code is woken up via AlarmManager . 主要的例外是你的代码是通过AlarmManager唤醒的。

It would be nice if ACTION_SCREEN_ON were a sticky broadcast, but that doesn't seem to be the case. 如果ACTION_SCREEN_ON是一个粘性广播会很好,但似乎并非如此。

If you can roll with Android 2.1 and newer, PowerManager now has an isScreenOn() method. 如果您可以使用Android 2.1及更高版本进行滚动,则PowerManager现在具有isScreenOn()方法。 The good news is, that's available. 好消息是,这是可用的。 The bad news is, since it was added there, it is unlikely that there is any other way to readily determine it. 坏消息是,由于它是在那里添加的,因此不太可能有任何其他方法可以轻易确定它。 And, a quick inspection of the source code doesn't give much hope that you might hack your way to the information in earlier SDKs. 而且,对源代码的快速检查并没有太多希望您可能会破解早期SDK中的信息。

The system deliberately doesn't allow applications to receive ACTION_SCREEN_ON and ACTION_SCREEN_OFF broadcasts in their manifest, because we very much do not want to have to launch a bunch of apps to turn the screen on or off. 系统故意不允许应用程序在其清单中接收ACTION_SCREEN_ONACTION_SCREEN_OFF广播,因为我们非常不希望启动一堆应用来打开或关闭屏幕。 This is something that happens all of the time, and must be as fast as possible. 这是在所有时间都发生的事情,并且必须尽可能快。

Unfortunately, as pointed out, there isn't a way to check immediately to see if the screen is on. 不幸的是,正如所指出的那样,没有办法立即检查屏幕是否打开。

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

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