android - 如何检测Android设备上的屏幕是否已经打开?

标签 android android-intent android-broadcast

我正在使用广播接收器来检测 SCREEN_ON 和 SCREEN_OFF 的时间。只有那一次我得到了值(value)。 但我想检测用户屏幕在设备上打开了多长时间?

有人能帮我解决这个问题吗?

最佳答案

我们可以为 Intent.ACTION_SCREEN_OFFIntent.ACTION_SCREEN_ON 使用广播来检查屏幕是打开还是关闭。

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
            // You can use Timer here to grab Time
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // AND DO WHATEVER YOU NEED TO DO HERE
            // You can use Timer here to grab Time
            wasScreenOn = true;
        }
    }

}

关于android - 如何检测Android设备上的屏幕是否已经打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25279213/

相关文章:

android - 收听新的日历事件

android - 星巴克在商店附近时如何在 Android 设备上显示 UI?

android - Android Studio中多个Dex文件异常

android - gridview 适配器上的 overridePendingTransition

android - Amazon Cognito 错误 : Unable to resolve host cognito-identity. us-east-1.amazonaws.com

android - 如何重新打开相同的 Activity/如何保持 Activity Activity ?

具有 Intent 的 Android onActivityResult()

java - 通知 Intent 不适用于 Android 应用程序

android - 隐式 Intent 是否会在内部进行广播?

Android WebView 显示纯文本而不是 html