简体   繁体   中英

Get all apps running time on android

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???

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));
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM