如何在Android的TextView中打印完整的logcat历史记录?

如何在Android的TextView中打印完整的logcat历史记录?,android,android-fragments,logging,logcat,android-logcat,Android,Android Fragments,Logging,Logcat,Android Logcat,我正在使用logcat在我的Android应用程序中记录消息,我希望能够访问这些消息,并在对话框片段的文本视图中显示它们。消息列表非常不一致,每次打开和关闭对话框时都会发生变化。它只显示一次完整的历史记录,然后在下次删除时显示,有时还会显示更多的消息。每次打开对话框时,我是否可以做些什么来显示所有消息(用于运行)?我是在错误的时间运行流程还是什么?或者应该在其他地方处理缓冲区?谢谢 public boolean onOptionsItemSelected(MenuItem item) {

我正在使用logcat在我的Android应用程序中记录消息,我希望能够访问这些消息,并在对话框片段的文本视图中显示它们。消息列表非常不一致,每次打开和关闭对话框时都会发生变化。它只显示一次完整的历史记录,然后在下次删除时显示,有时还会显示更多的消息。每次打开对话框时,我是否可以做些什么来显示所有消息(用于运行)?我是在错误的时间运行流程还是什么?或者应该在其他地方处理缓冲区?谢谢

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_view_logs:
            // View Logs MenuItem tapped
            if (logsDialogFragment == null) {
                logsDialogFragment = new LogsDialogFragment();
            }
            logsDialogFragment.show(getFragmentManager(), "dialog");
            return true;
        case R.id.action_exit:
            // Exit MenuItem tapped
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

请在清单中为logcat添加权限

用于消息的logcat缓冲区不是很大,所以旧消息只需从logcat中删除,如果您需要完整的日志-将logcat消息保存到SD卡上的文件中,然后从中读取信息。此方法将所有当前消息保存到文件:

public static void saveLogcatToFile(Context context) {
        String fileName = "yourlogname.log";
        File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/YourAppName", fileName);
        try {
            @SuppressWarnings("unused")
            Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

上面的代码只是某个时间点日志的快照。如果要连续写入日志,请使用Java的Logging w/a FileLogger并写入日志。

我最终将历史记录保存到一个成员变量,并且每次打开对话框时只追加新的日志行

public class LogsDialogFragment extends DialogFragment {

    private StringBuilder log = new StringBuilder();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Sets the Layout for the UI
        LayoutInflater i = getActivity().getLayoutInflater();
        View rootView = i.inflate(R.layout.fragment_logs_dialog, null);

        TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView);
        logTextView.setMovementMethod(new ScrollingMovementMethod());

        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(WifiDirectHandler.LOG_TAG)){
                    // Removes log tag and PID from the log line
                    log.append(line.substring(line.indexOf(": ") + 2)).append("\n");
                }
            }

            this.log.append(log.toString().replace(this.log.toString(), ""));
            logTextView.setText(this.log.toString());
        } catch (IOException e) {
            Log.e("wifiDirectHandler", "Failure reading logcat");
        }

        // Creates and returns the AlertDialog for the logs
        AlertDialog.Builder dialogBuilder =  new  AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.title_logs))
            .setNegativeButton(getString(R.string.action_close),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }
            ).setView(rootView);
        return dialogBuilder.create();
    }
}

logTextView
是否显示结果?是的,但每次我打开和关闭对话框时,结果都不同。结果不一样,很自然,Log cat显示不同,请参阅详细信息,您可能不会得到相同的结果。。
public static void saveLogcatToFile(Context context) {
        String fileName = "yourlogname.log";
        File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/YourAppName", fileName);
        try {
            @SuppressWarnings("unused")
            Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public class LogsDialogFragment extends DialogFragment {

    private StringBuilder log = new StringBuilder();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Sets the Layout for the UI
        LayoutInflater i = getActivity().getLayoutInflater();
        View rootView = i.inflate(R.layout.fragment_logs_dialog, null);

        TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView);
        logTextView.setMovementMethod(new ScrollingMovementMethod());

        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(WifiDirectHandler.LOG_TAG)){
                    // Removes log tag and PID from the log line
                    log.append(line.substring(line.indexOf(": ") + 2)).append("\n");
                }
            }

            this.log.append(log.toString().replace(this.log.toString(), ""));
            logTextView.setText(this.log.toString());
        } catch (IOException e) {
            Log.e("wifiDirectHandler", "Failure reading logcat");
        }

        // Creates and returns the AlertDialog for the logs
        AlertDialog.Builder dialogBuilder =  new  AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.title_logs))
            .setNegativeButton(getString(R.string.action_close),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }
            ).setView(rootView);
        return dialogBuilder.create();
    }
}