如何在Android上跟踪“屏幕开启”时间?

前端之家收集整理的这篇文章主要介绍了如何在Android上跟踪“屏幕开启”时间?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用我的第一个应用程序(理论上)做一些简单的事情,但我需要一些帮助才能开始.我想构建一个应用程序,告诉我今天Nexus 4上的屏幕有多长时间了.

Android显示自上次通过设置>以来屏幕已经显示时间长度电池>屏幕,但我不知道如何解析这些数据,或者如何将其限制在当天.
到目前为止,我的研究表明,batterystats.bin涉及上面概述的屏幕跟踪,我可以使用ACTION_SCREEN_ON和ACTION_SCREEN_OFF,但我不确定这是否正是我正在寻找的.

有任何想法吗?

解决方法

我知道这是一个老问题,但希望这会对某人有所帮助.

在我的服务中,我监听SCREEN_ON / SCREEN_OFF操作并获取两次之间的差异并保存到onDestroy回调中的sharedpreferences.

  1. BroadcastReceiver mybroadcast = new BroadcastReceiver() {
  2.  
  3. @Override
  4. public void onReceive(Context context,Intent intent) {
  5. Log.i("[BroadcastReceiver]","MyReceiver");
  6.  
  7. if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
  8. startTimer = System.currentTimeMillis();
  9. }
  10. else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
  11. endTimer = System.currentTimeMillis();
  12. screenOnTime = endTimer - startTimer;
  13.  
  14. if(screenOnTime < TIME_ERROR) {
  15. times.add(screenOnTime);
  16. }
  17.  
  18. }
  19.  
  20. }
  21. };

该服务在启动时和更换包时启动(我发现它对调试很有帮助).

  1. public class BootReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context,Intent intent) {
  4. Intent i = new Intent(context,ScreenOnService.class);
  5. context.startService(i);
  6. }
  7. }

这是我对广播接收器的表现

  1. <receiver android:name=".BootReceiver">
  2. <intent-filter>
  3. <action android:name="android.intent.action.BOOT_COMPLETED" />
  4. </intent-filter>
  5. <intent-filter>
  6. <action android:name="android.intent.action.PACKAGE_REPLACED" />
  7. <data android:scheme="package" android:path="com.application.test" />
  8. </intent-filter>
  9. </receiver>

猜你在找的Android相关文章