简体   繁体   中英

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.

I've found a solution on API Level 7. It is easy to develop :

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

But I need a solution for 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. 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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