以下是我用于获取每个已安装应用程序的UsageStats
列表的代码。
List<UsageStats> usageStatsList =
getUsageStatistics(UsageStatsManager.INTERVAL_DAILY);
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
Collections.sort(usageStatsList, new SortListComparatorDesc());
这是SortListComparotorDesc类。
import android.annotation.TargetApi;
import android.app.usage.UsageStats;
import android.os.Build;
import java.util.Comparator;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class SortListComparotorDesc implements Comparator<UsageStats> {
@Override
public int compare(UsageStats left, UsageStats right) {
return (int) (right.getLastTimeUsed() - left.getLastTimeUsed());
// return Long.compare(right.getLastTimeUsed(), left.getLastTimeUsed());
}
}
这是我的Logcat:
02-13 17:39:24.774 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 675786
02-13 17:39:24.783 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 2560117
02-13 17:39:24.793 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 503115
02-13 17:39:24.802 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 0
02-13 17:39:24.810 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 2016255
02-13 17:39:24.819 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 277573
02-13 17:39:24.828 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 27227
02-13 17:39:27.880 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 90220
02-13 17:39:29.483 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 257245
02-13 17:39:32.668 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 5199
02-13 17:39:32.951 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 1364625
02-13 17:39:35.295 5029-5029/com.app.usagelistapp E/TimeInforground: Time in Foreground = 99522