gpt4 book ai didi

android - 如何判断手机是在使用中还是待机中?

转载 作者:行者123 更新时间:2023-11-29 19:03:43 24 4
gpt4 key购买 nike

我正在开发一款研究应用程序,用于确定研究对象的手机使用行为并绘制图表。我想知道手机屏幕何时亮起,是因为用户打开它还是因为外部刺激,例如有电话或消息进来。

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

最佳答案

要了解何时开/关屏幕,您可以使用下一个接收器,例如:

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/

(或)您可以使用 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?

最后两个可能重复

但在任何情况下,您都需要一些后台工作人员,这将获取有关打开/关闭屏幕以及打开/关闭屏幕的事件的信息。为此,您可以使用另一个接收器,它会在调用电话或其他东西时接收事件,并存储信息,如:“事件时间”、“事件时间”......在一些存储中,如列表。在这种情况下,您可以获取有关打开/关闭屏幕的内容的信息。

关于android - 如何判断手机是在使用中还是待机中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com