如何获得屏幕打开的时间

时间:2014-04-24 04:27:01

标签: android

我正在开发一个Android应用程序,如果屏幕长时间处于打开状态,它将警告用户。 该应用程序背后的基本思想是警告用户眼睛长时间暴露在屏幕光线下。 我基本上想要打开屏幕灯的时间。 如果达到某个限制,则会通知用户将屏幕关闭一段时间。

  

如何获得打开屏幕的时间?

2 个答案:

答案 0 :(得分:5)

阅读文档......

public static final String ACTION_SCREEN_OFF

Added in API level 1
Broadcast Action: Sent after the screen turns off.

This is a protected intent that can only be sent by the system.

Constant Value: "android.intent.action.SCREEN_OFF"

public static final String ACTION_SCREEN_ON
Added in API level 1
Broadcast Action: Sent after the screen turns on.

This is a protected intent that can only be sent by the system.

Constant Value: "android.intent.action.SCREEN_ON"

当屏幕开启时,跟踪当前时间,然后设置一段时间的警报,然后警告用户。

答案 1 :(得分:0)

您可以使用设备时间跟踪用户,并可以将其保存在SharedPreferences中以进行进一步检查。

获取系统时间,如..

// using Calendar class
Calendar ci = Calendar.getInstance();

String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" + 
    (ci.get(Calendar.MONTH) + 1) + "-" +
    ci.get(Calendar.DAY_OF_MONTH) + " " +
    ci.get(Calendar.HOUR) + ":" +
    ci.get(Calendar.MINUTE) +  ":" +
    ci.get(Calendar.SECOND);

在堆栈和谷歌上有很多答案可供选择。

祝你好运!