Reputation: 115
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?
Is there any API
for this or we have to develop are own logic to make it work???
Upvotes: 3
Views: 4598
Reputation: 8371
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 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));
}
Upvotes: 3