【问题标题】:Time that an application is running应用程序运行的时间
【发布时间】:2010-10-11 05:12:31
【问题描述】:

如何确定应用程序运行了多长时间?每次启动和关闭应用程序时都有广播吗?

我正在尝试记录使用应用程序所花费的时间。

【问题讨论】:

  • 没有广播,但只要活动开始或停止活动,或被拆除,就会调用活动生命周期方法(例如“onResume”、“onPause”和“onDestroy”)。跨度>
  • @Shoan 请检查我的答案

标签: android


【解决方案1】:

如果您有兴趣记录自己的应用程序的使用情况,可以使用System.currentTimeMillis() 获取开始时间,并将其与应用程序关闭时返回的值进行比较。

要获得应用程序启动的整个时间,您需要区分最重要的 Activity 类的 onCreate()onDestroy()。仅当应用程序处于前台时,在每个 Activity 类中使​​用 onResume()onPause() 并对每个类的时间求和。

【讨论】:

  • 是否有可能知道别人的应用在手机上运行了多长时间?例如:gmail 应用在手机上运行了多长时间?
  • @Shoan 你应该为此发布一个新问题 - 似乎是一个很受欢迎的评论。
  • @donturner 我只是在澄清问题的意图。我应该改写一下并将其添加到问题中吗?
  • @Shoan 抱歉没有意识到你是 OP。此答案不涵盖其他应用程序,因此不能完全回答您的原始问题。我会不接受它,看看是否有其他人可以更彻底地回答您的问题。
【解决方案2】:

代码:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

long currentMillis = Calendar.getInstance().getTimeInMillis();
Calendar cal = Calendar.getInstance();

for (ActivityManager.RunningServiceInfo info : services) {
  cal.setTimeInMillis(currentMillis-info.activeSince);

  Log.i("TAG", String.format("Process %s has been running since: %d ms",info.process,  info.activeSince));
}

Logcat:

TAG: Process com.android.bluetooth has been running since: 26526 ms
TAG: Process com.qualcomm.telephony has been running since: 68521 ms
TAG: Process com.motorola.ccc has been running since: 57456 ms
TAG: Process com.google.android.music:main has been running since: 26245 ms
TAG: Process com.android.phone has been running since: 29421 ms
TAG: Process com.motorola.ccc has been running since: 52141 ms
TAG: Process system has been running since: 28602 ms
TAG: Process com.motorola.actions has been running since: 74371 ms
TAG: Process com.motorola.ccc has been running since: 59166 ms
TAG: Process com.motorola.process.slpc has been running since: 25483 ms
TAG: Process com.android.systemui has been running since: 30142 ms
TAG: Process com.android.bluetooth has been running since: 22187 ms
TAG: Process system has been running since: 28603 ms
TAG: Process com.google.android.gms.persistent has been running since: 31621 ms
TAG: Process com.android.systemui has been running since: 27361 ms
TAG: Process com.google.android.gms.persistent has been running since: 99678 ms
TAG: Process com.motorola.contacts.preloadcontacts has been running since: 45603 ms
TAG: Process com.google.android.gms.persistent has been running since: 73457 ms
TAG: Process com.google.android.gms.persistent has been running since: 72908 ms
TAG: Process com.google.android.gms.persistent has been running since: 37251 ms
TAG: Process com.motorola.modemservice has been running since: 37049 ms
TAG: Process .esfm has been running since: 44187 ms
TAG: Process com.google.android.gms has been running since: 14875780 ms
TAG: Process com.google.android.gms.persistent has been running since: 94606 ms
TAG: Process com.motorola.ccc has been running since: 66227 ms
TAG: Process com.android.bluetooth has been running since: 31682 ms
TAG: Process com.motorola.modemservice has been running since: 37051 ms
TAG: Process com.android.bluetooth has been running since: 32869 ms
TAG: Process com.google.android.gms.persistent has been running since: 99691 ms
TAG: Process com.motorola.ccc has been running since: 92883 ms
TAG: Process com.motorola.cameraone has been running since: 75193 ms
TAG: Process com.motorola.ccc has been running since: 57465 ms
TAG: Process com.google.android.gms.persistent has been running since: 74054 ms
TAG: Process com.estrongs.android.pop has been running since: 42158 ms
TAG: Process com.motorola.process.system has been running since: 27632 ms
TAG: Process com.android.bluetooth has been running since: 26540 ms
TAG: Process com.google.android.gms.persistent has been running since: 42027 ms
TAG: Process com.estrongs.android.pop has been running since: 42334 ms
TAG: Process com.google.android.gms.persistent has been running since: 99627 ms
TAG: Process com.motorola.slpc has been running since: 25575 ms
TAG: Process com.snaptube.premium has been running since: 42312 ms
TAG: Process com.android.bluetooth has been running since: 27324 ms
TAG: Process com.estrongs.android.pop has been running since: 10845469 ms
TAG: Process com.qualcomm.qcrilmsgtunnel has been running since: 29328 ms
TAG: Process com.motorola.config.wifi has been running since: 50966 ms
TAG: Process com.google.android.gms has been running since: 13422977 ms
TAG: Process com.motorola.modemservice has been running since: 67877 ms
TAG: Process com.google.android.gms.persistent has been running since: 99699 ms
TAG: Process com.motorola.process.system has been running since: 93567 ms
TAG: Process com.android.systemui has been running since: 25460 ms
TAG: Process com.google.android.talk has been running since: 14632240 ms
TAG: Process system has been running since: 27605 ms
TAG: Process com.estrongs.android.pop:ka has been running since: 42437 ms
TAG: Process system has been running since: 28600 ms
TAG: Process com.google.android.gms.persistent has been running since: 40725 ms
TAG: Process com.motorola.slpc has been running since: 25556 ms
TAG: Process com.motorola.ccc has been running since: 57484 ms
TAG: Process com.google.android.gms.persistent has been running since: 35677 ms
TAG: Process com.google.android.gms.persistent has been running since: 32947 ms
TAG: Process com.snaptube.premium has been running since: 123580 ms
TAG: Process com.google.android.gms.persistent has been running since: 94452 ms
TAG: Process system has been running since: 28244 ms
TAG: Process com.estrongs.android.pop has been running since: 41920 ms
TAG: Process com.motorola.process.system has been running since: 68805 ms
TAG: Process system has been running since: 29054 ms
TAG: Process com.google.android.gms.persistent has been running since: 33060 ms
TAG: Process com.motorola.ccc has been running since: 52153 ms
TAG: Process com.google.android.gms.persistent has been running since: 31658 ms
TAG: Process com.google.android.gms.persistent has been running since: 28665 ms
TAG: Process com.google.android.gms.persistent has been running since: 30579 ms
TAG: Process com.google.android.inputmethod.latin has been running since: 27422 ms
TAG: Process com.motorola.motocare has been running since: 68330 ms
TAG: Process system has been running since: 25415 ms
TAG: Process com.motorola.ccc has been running since: 59215 ms
TAG: Process android.process.media has been running since: 13885530 ms
TAG: Process com.google.android.gms.persistent has been running since: 74301 ms
TAG: Process com.motorola.actions has been running since: 73981 ms
TAG: Process com.android.bluetooth has been running since: 27310 ms
TAG: Process com.motorola.process.system has been running since: 68538 ms
TAG: Process com.google.android.googlequicksearchbox:interactor has been running since: 27343 ms
TAG: Process system has been running since: 24919 ms
TAG: Process com.google.android.apps.messaging:rcs has been running since: 69945 ms
TAG: Process com.google.android.gms.persistent has been running since: 73045 ms
TAG: Process com.google.android.gms.persistent has been running since: 28213 ms
TAG: Process com.google.android.gms.persistent has been running since: 99706 ms
TAG: Process com.snaptube.premium has been running since: 39033 ms
TAG: Process com.android.bluetooth has been running since: 26533 ms
TAG: Process com.android.phone has been running since: 29411 ms
TAG: Process com.motorola.process.system has been running since: 94766 ms
TAG: Process com.snaptube.premium has been running since: 14530932 ms
TAG: Process com.android.bluetooth has been running since: 36311 ms
TAG: Process com.android.bluetooth has been running since: 26529 ms
TAG: Process com.google.android.gms.persistent has been running since: 24921 ms
TAG: Process com.google.android.gms has been running since: 90390 ms
TAG: Process org.mozilla.firefox has been running since: 80946 ms
TAG: Process com.motorola.actions has been running since: 73986 ms

输出:

【讨论】:

  • 这仅适用于服务,它不再起作用,因为它只会返回当前应用程序。如果应用没有服务,它不会返回任何东西......
【解决方案3】:
 long millisSinceBoot = SystemClock.elapsedRealtime();
         System.out.println("00000    "+millisSinceBoot);
         System.out.println("11111    "+values.get(position).activeSince+" "+values.get(position).process);
         long time = ((millisSinceBoot - values.get(position).activeSince)/1000);
         int[] vals = splitToComponentTimes(time);

         String time1 = vals[0]+":"+vals[1]+":"+vals[2];
         time_list.add(time);        
         timer.setText(time1);

         System.out.println("valssssss "+vals[0]+" "+vals[1]);

【讨论】:

    猜你喜欢
    • 2018-02-24
    • 2013-08-05
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多