How to list all installed applications with total running time of each app and show it?
CodePudding user response:
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
}
}
CodePudding user response:
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));
}