从logcat输出重写代码到TextView输出

时间:2018-04-24 10:57:34

标签: java android logging textview

我想重写以下代码以在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");
        }
    }
}

由于

1 个答案:

答案 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");
        }
    }
}