Hiii, I am working on an app in which i have to get the running time
of all the apps that are installed on the device. So, is it possible to know how long somebody else's app is running for on the phone? Ex: How long is the gmail app running on the phone?
Hiii,我正在研究一个应用程序,其中我必须获得设备上安装的所有应用程序的运行时间。那么,有可能知道其他人的应用程序在手机上运行多长时间了吗?例如:手机上运行的Gmail应用程序有多长时间了?
Is there any API
for this or we have to develop are own logic to make it work???
是否有任何API或我们必须开发自己的逻辑,使其工作?
1 个解决方案
#1
2
You can get a list of each service's start time by using ActivityManager.RunningServiceInfo.activeSince, described here. Here's a snippet that retrieves the times for all service processes.
您可以使用此处描述的ActivityManager.RunningServiceInfo.activeSince获取每个服务的开始时间列表。这是一个片段,用于检索所有服务进程的时间。
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 with component %s has been running since %s (%d milliseconds)",
info.process, info.service.getClassName(), cal.getTime().toString(), info.activeSince));
}
#1
2
You can get a list of each service's start time by using ActivityManager.RunningServiceInfo.activeSince, described here. Here's a snippet that retrieves the times for all service processes.
您可以使用此处描述的ActivityManager.RunningServiceInfo.activeSince获取每个服务的开始时间列表。这是一个片段,用于检索所有服务进程的时间。
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 with component %s has been running since %s (%d milliseconds)",
info.process, info.service.getClassName(), cal.getTime().toString(), info.activeSince));
}