简体   繁体   中英

Android: List all installed apps and get total running time of each app

How to list all installed applications with total running time of each app and show it?

Add this permission to your Android Manifest file:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

And after that you can get a list of apps installed with this function:

public static List <ApplicationInfo> getUserInstalledApplications(Context context) {
    final PackageManager packageManager = context.getPackageManager();
    List < ApplicationInfo > installedApplications =
        packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo appInfo: installedApplications) {
        //Do what you want with app info
    }
}

You can get a list of each service's start time by using 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));
 }

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