如何确定手机是否正在使用或处于待机状态?

时间:2017-12-25 06:44:52

标签: android

我正在开发一个研究应用程序,用于确定和绘制研究中受试者的电话使用行为。 我想知道电话屏幕何时亮起,是因为打开电话的用户还是因为外部刺激,例如打来电话或留言。

我似乎无法在Android开发者或Google上找到有关此内容的任何信息。这有可能实现吗?

1 个答案:

答案 0 :(得分:2)

要知道何时开启/关闭屏幕,您可以使用下一个接收器,例如:

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;
    }
}
}

从这里开始:https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

(OR)您可以使用PowerManager

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

从这里开始:how to check screen on/off status in onStop()?

在这里:How can I tell if the screen is on in android?

最后两个可能重复

但无论如何你需要一些后台工作人员,这将获取有关开/关屏幕和打开/关闭屏幕的事件的信息。为此,您可以使用另一个接收电话时接收事件的接收器,并存储信息,如:"事件时间","事件时间" ...在某些像列表一样存储。 在这种情况下,您可以了解屏幕上打开/关闭的内容。