For knowing when on/off screen is happened you can use next receiver for example:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}
}
from here: https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
(OR) You can use PowerManager
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm. isInteractive();
From here: how to check screen on/off status in onStop()?
And here: How can I tell if the screen is on in android?
Last two possibly duplicate
But in any case you need some background worker, that would be take information about on/off screen and events that turning on/off your screen. For that you can use another receiver that would receive events when phone was called or something else, and store info like: "event-time", "event-time"... in some storage like a list.
In this case you can take info about what called on/off your screen.