Andorid - screen on or off, outdated isScreenOn ()

I need to know if the user is interacting with the device, so I used this following code. I don’t know if it’s normal to use it Build.VERSION.SDK_INTso that it works on all versions.?

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        if (pm.isInteractive()) {
            System.out.println("alarm screen ON");
        }
    }
    else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH){
        if(pm.isScreenOn()){
            System.out.println("alarm screen ON");
        }
    }

    else {
        System.out.println("alarm screen OFF");
    }

separated method: isScreenOn()

+4
source share

All Articles