尝试确定屏幕是否关闭时检测始终显示 [英] Detect Always-On Display when trying to determine if the screen is off
问题描述
我试图弄清楚如何检测Android上的显示器是否关闭,并且我感觉S10上的Always-On显示器正在影响我的结果.
I'm trying to figure out how to detect if the Display is Off on Android and I have a feeling that the Always-On Display on my S10 is affecting my results.
当前,我正在使用此stackoverflow答案中的逻辑.
Currently, I'm using the logic found in this stackoverflow answer.
还有评论在该线程上,表示您应该能够使用 c0> 检查三星的"Always-On Display"(始终显示),但没有说明如何操作.
There is also a comment on that thread that says you should be able to use Display.FLAG_PRIVATE
to check for Samsung's Always-On Display, but it doesn't explain how.
使用该信息,我将其组合在一起.但这似乎不起作用.如果我启用了Always-On显示屏,则该显示屏似乎总是被视为常亮.
Using that information, I've put together this function. But it doesn't seem to work. The display seems to be counted as on almost always if i have the Always-On display enabled.
public boolean isScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if ((display.getFlags() & Display.FLAG_PRIVATE) != Display.FLAG_PRIVATE) {
if (display.getState() != Display.STATE_OFF) {
return true;
}
}
}
return false;
} else {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//noinspection deprecation
return pm.isScreenOn();
}
}
我不确定我检查Display.FLAG_PRIVATE
标志的方式是否正确,或者是否甚至可以帮助Always-On屏幕显示影响结果的可能性.
I'm not sure if the way i'm checking the Display.FLAG_PRIVATE
flag is correct, or if that will even help with the potential of the Always-On display affecting the result.
推荐答案
我发现这些标志实际上与它没有任何关系.似乎显示状态将是以下几种状态之一,包括 Display.STATE_DOZE_SUSPENDED
, Display.STATE_DOZE
等常亮显示屏处于活动状态.因此,我只是检查 Display.STATE_ON
并将所有其他状态都视为显示器处于关闭状态.
I found that the flags don't actually have anything to do with it. It seems that the display state will be one of several states including Display.STATE_DOZE_SUSPENDED
, Display.STATE_DOZE
and several others when the Always-On Display is active. So instead, I'm just checking for Display.STATE_ON
and treating all other states as the display being turned off.
public boolean isScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() == Display.STATE_ON) {
return true;
}
}
return false;
} else {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//noinspection deprecation
return pm.isScreenOn();
}
}
这篇关于尝试确定屏幕是否关闭时检测始终显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!