我可以使用 UsgaeStatsManager 获取应用程序的使用统计信息,如下所示
UsageStatsManager usm = getUsageStatsManager(context);
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.DATE, -1);
long startTime = calendar.getTimeInMillis();
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime,endTime);
for (UsageStats u : usageStatsList){
Log.d(TAG, "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: "
+ u.getTotalTimeInForeground() + " milliseconds") ;
}
当我得到输出时,它如下所示:
Pkg:com.skype.raiderForeground used time in ms:23922
Pkg:com.google.android.youtubeForeground used time in ms:0
Pkg:com.sec.android.app.chromecustomizationsForeground used time in ms:0
Pkg:com.whatsappForeground used time in ms:1665723
.
.
.
这是正确的,但我想得到这个列表,其中包含更多用户可读格式的应用程序名称,例如
Pkg:skype Foreground used time in ms:23922
Pkg:youtube Foreground used time in ms:0
Pkg:whatsapp Foreground used time in ms:1665723
我想知道我们是否有任何类似于 api applicationInfo.loadLabel(getPackageManager()).toString(); 的 api 在 PackageManager 中,它为用户提供可读的包名称,而不是包的技术名称。或者,有没有其他方法可以打印包的简单名称?