如何在Android的TextView中打印完整的logcat历史记录? [英] How can I print the full logcat history in a TextView in Android?
问题描述
我正在使用logcat在我的Android应用程序中记录消息,并且我希望能够访问这些消息并在DialogFragment的TextView中显示它们.消息列表非常不一致,每次打开和关闭对话框时,消息列表都会更改.它会显示一次完整的历史记录,然后在下次擦除时显示,有时还会显示更多消息.每次打开对话框时,我有什么办法可以显示所有消息(用于运行)?我是在错误的时间运行流程吗?还是应该在其他地方处理缓冲区?谢谢.
I'm using logcat to log messages in my Android application, and I'd like to be able to access these messages and show them in a TextView in a DialogFragment. The list of messages is very inconsistent and changes each time I open and close the dialog. It shows the full history once, and then the next time it erases, and sometimes it shows some more messages. Is there anything that I can do to show all the messages (for the run) each time I open the dialog? Am I running the process at the wrong time or something? Or should the buffer be handles somewhere else? Thanks.
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);
}
}
public class LogsDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.i(WifiDirectHandler.LOG_TAG, "Viewing 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();
}
}
);
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");
}
}
logTextView.setText(log.toString());
} catch (IOException e) {
}
dialogBuilder.setView(rootView);
return dialogBuilder.create();
}
}
推荐答案
我最终将历史记录保存到成员变量中,并且每次打开对话框时都仅追加新的日志行.
I ended up saving the history to a member variable and only appending the new log lines each time I open the dialog.
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();
}
}
这篇关于如何在Android的TextView中打印完整的logcat历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!