Reputation: 600
I am working on a research app that determines and graphs the phone use behavior of subjects in the study. I want to know when the phone screen goes on, whether it is because user who turned it on or if it was because an external stimulus, e.g. a phone call or message came in.
I can't seem to find any information on this on the Android developer or Google. Is this possible to accomplish this?
Upvotes: 1
Views: 238
Reputation: 1705
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.
Upvotes: 2