简体   繁体   English

如果API级别为4,如何检测屏幕是打开还是关闭?

[英]How to detect whether screen is on or off if API level is 4?

I am wondering to know how to detect screen dim or brightness on Android 1.6. 我想知道如何在Android 1.6上检测屏幕暗淡或亮度。

I've found a solution on API Level 7. It is easy to develop : 我在API Level 7上找到了一个解决方案。它很容易开发:

PowerManager pm = (PowerManager)
getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();

But I need a solution for Android 1.x. 但我需要Android 1.x的解决方案。

Can you suggest me ? 你能建议我吗?

Thanks. 谢谢。

对于屏幕开关状态,您可以尝试使用ACTION_SCREEN_ONACTION_SCREEN_OFF Intent ,如本博客文章所示: http//thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-上意图/

The approach with the ACTION_SCREEN_ON did not work for me. 使用ACTION_SCREEN_ON的方法对我不起作用。 After some different solutions this code finally solved the problem for me: 经过一些不同的解决方案,这段代码终于解决了我的问题:

/**
 * Is the screen of the device on.
 * @param context the context
 * @return true when (at least one) screen is on
 */
public boolean isScreenOn(Context context) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //noinspection deprecation
        return pm.isScreenOn();
    }
}

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

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