我想重写以下代码以在TextView而不是logcat中输出。我该怎么做?
public static void printUsageStats(List<UsageStats> usageStatsList,Context context){
for (UsageStats u : usageStatsList) {
PackageManager pm = context.getPackageManager();
PackageInfo foregroundAppPackageInfo = null;
try {
foregroundAppPackageInfo = pm.getPackageInfo(u.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (u.getTotalTimeInForeground() > 0) {
Log.d(TAG, "Pkg: " + foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString() + "\t"
+ String.valueOf(((u.getTotalTimeInForeground() / 60000) / 60) / 24) + "d "
+ String.valueOf(((u.getTotalTimeInForeground() / 60000) / 60) % 24) + "hr "
+ String.valueOf((u.getTotalTimeInForeground() / 60000) % 60) + "m "
+ String.valueOf((u.getTotalTimeInForeground() / 1000) % 60) + "s");
}
}
}
由于
答案 0 :(得分:0)
只需将Log.d(TAG, "Your Text");
更改为textView.setText("Your Text");
应该像这样工作:
public static void printUsageStats(List<UsageStats> usageStatsList,Context context, TextView textView){
for (UsageStats u : usageStatsList) {
PackageManager pm = context.getPackageManager();
PackageInfo foregroundAppPackageInfo = null;
try {
foregroundAppPackageInfo = pm.getPackageInfo(u.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (u.getTotalTimeInForeground() > 0) {
textView.setText("Pkg: " + foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString() + "\t"
+ String.valueOf(((u.getTotalTimeInForeground() / 60000) / 60) / 24) + "d "
+ String.valueOf(((u.getTotalTimeInForeground() / 60000) / 60) % 24) + "hr "
+ String.valueOf((u.getTotalTimeInForeground() / 60000) % 60) + "m "
+ String.valueOf((u.getTotalTimeInForeground() / 1000) % 60) + "s");
}
}
}