我有一段代码,允许我检索我的Android设备上运行的所有应用/服务,包括我的应用程序
我正在建设。我不完全确定我是否在正确的道路上,但因为我在Android 4.3上调试我想使用ActivityManager.RunningService.activeSince
(每个服务/应用程序)并从SystemClock.elapsedRealtime();
中减去它,我理解的是重启后的总毫秒数。所以举个例子
如果该设备在上午10点重新进行重新绑定并且whatsapp在10:15开始并且当前时间是1030我希望能够使用这些值
得到whatsapp花费的近似估计。我觉得这不是实现这一目标的最优雅方式,因此我非常乐意
任何建议。这是我的代码到目前为止。现在我使用的是android 4.3
ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo info : services) {
cal.setTimeInMillis(currentMillis-info.activeSince);
long millisSinceBoot = SystemClock.elapsedRealtime();
long appStartTime = info.activeSince;
long appDuration = appStartTime - millisSinceBoot ;
//long time = ((millisSinceBoot - values.get(position).activeSince)/1000);
//long time = ((millisSinceBoot - currentMillis-info.activeSince)/1000);
//Log.i("HRHHRHRHRHR", "%%%%%%%%%%%%%%%%"+time);
//String time1 = String.valueOf(time);
int seconds = (int) (appDuration / 1000) % 60 ;
int minutes = (int) ((appDuration / (1000*60)) % 60);
int hours = (int) ((appDuration / (1000*60*60)) % 24);
String time11 = hours+":"+minutes+":"+seconds;
Log.i("Time", "Secs:- " + seconds + " " + "Mins:- " + minutes + " " + "Hours:- " + hours);
Log.i(TAG, String.format("Process %s with component %s has been running since %s (%d milliseconds)",
info.process, info.service.getClassName(), cal.getTime().toString(), info.activeSince ));
}