Android: 如何在 UsageStats 中获取应用程序标签
Android: how to get application label in UsageStats
我可以使用 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 类似于 api applicationInfo.loadLabel(getPackageManager()).toString();在 PackageManager 中,它为用户提供可读的包名称而不是包的技术名称。或者,有没有其他方法可以打印包的简单名称?
您可以使用此方法通过包名称获取 AppName
private String getAppNameFromPackage(String packageName, Context context) {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager()
.queryIntentActivities(mainIntent, 0);
for (ResolveInfo app : pkgAppsList) {
if (app.activityInfo.packageName.equals(packageName)) {
return app.activityInfo.loadLabel(context.getPackageManager()).toString();
}
}
return null;
}
你可以像这样打印for循环,
for (UsageStats u : usageStatsList){
Log.d(TAG, "Pkg: " + getAppNameFromPackage(u.getPackageName(), context) + "\t" + "ForegroundTime: "
+ u.getTotalTimeInForeground() + " milliseconds") ;
}
它会给出这样的结果
Pkg:skype Foreground used time in ms:23922
Pkg:youtube Foreground used time in ms:0
Pkg:whatsapp Foreground used time in ms:1665723
我可以使用 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 类似于 api applicationInfo.loadLabel(getPackageManager()).toString();在 PackageManager 中,它为用户提供可读的包名称而不是包的技术名称。或者,有没有其他方法可以打印包的简单名称?
您可以使用此方法通过包名称获取 AppName
private String getAppNameFromPackage(String packageName, Context context) {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager()
.queryIntentActivities(mainIntent, 0);
for (ResolveInfo app : pkgAppsList) {
if (app.activityInfo.packageName.equals(packageName)) {
return app.activityInfo.loadLabel(context.getPackageManager()).toString();
}
}
return null;
}
你可以像这样打印for循环,
for (UsageStats u : usageStatsList){
Log.d(TAG, "Pkg: " + getAppNameFromPackage(u.getPackageName(), context) + "\t" + "ForegroundTime: "
+ u.getTotalTimeInForeground() + " milliseconds") ;
}
它会给出这样的结果
Pkg:skype Foreground used time in ms:23922
Pkg:youtube Foreground used time in ms:0
Pkg:whatsapp Foreground used time in ms:1665723