Android Dev/Android

[Android] How to check screen off status in onStop()

서메리 2019. 12. 19. 17:19

1. onStop() in Activity

@Override
public void onStop() {
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = Utils.hasLollipop() ? pm.isInteractive() : pm.isScreenOn();
    if (isScreenOn) {
    	// your code
    }
    super.onStop();
}

 

 

2. hasLollipop() in Utils class

public static boolean hasLollipop() {
    return Build.VERSION.SDK_INT >= 21;
}