我正试图找到一种在活动中的TextView中显示日志消息的好方法。我在TextView中使用了tried to display logcat messages,但每次打开和关闭Fragment时结果都不一样,并且并不是显示所有消息。我正在登录多个活动和类,所以我试图找到一种方法来记录,而不是将它连接到每个类。有没有替代logcat或什么的,我可以访问和显示的某种类型的系统日志记录?或者任何有用的库或任何东西?我应该如何登录Android的用户界面?
感谢
我正试图找到一种在活动中的TextView中显示日志消息的好方法。我在TextView中使用了tried to display logcat messages,但每次打开和关闭Fragment时结果都不一样,并且并不是显示所有消息。我正在登录多个活动和类,所以我试图找到一种方法来记录,而不是将它连接到每个类。有没有替代logcat或什么的,我可以访问和显示的某种类型的系统日志记录?或者任何有用的库或任何东西?我应该如何登录Android的用户界面?
感谢
我最终将历史记录保存到成员变量,并且每次打开对话框时只添加新的日志行。
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();
}
}
这只对偶发事件有用 - 除此之外它变得非常烦人,并且实际上是无用的。 –
谢谢,但我很有兴趣查看会话中记录的所有消息的列表。 – Brendan
当谈到登录Android上我肯定会推荐: https://github.com/JakeWharton/timber
例如,你不需要像Tim一样随时做烦人的TAG ber为你做这个。
关于你的问题,我想你可以做什么来种植你自己的树在木材。这将确保所有日志都通过那里,并从那里你可以捕捉并记录你想要的一切。查看他在应用程序中植树的示例:https://github.com/JakeWharton/timber/blob/master/timber-sample/src/main/java/com/example/timber/ExampleApp.java 现在,它位于应用程序层,因此您需要以某种方式将它们提取到您的活动中。一种快捷的方式,不是非常美丽的方式,我认为你可以做的就是将日志存储在一个集合中,并且可以静态访问该应用程序中的日志。
在附注上,我很好奇你为什么要这样做,为什么Android Studio的记录工具不够好? :)
谢谢我会检查出来。 Android Studio的日志记录工具非常好用,但是当我在没有电脑的情况下运行应用程序时,我也希望能够查看设备上的日志。这是一个网络测试应用程序,所以我在后台发生某些事情时记录消息,并且希望能够查看这些消息的列表。 – Brendan
嘿布伦丹,当然有道理。我认为木材将满足您的需求。如果不是另一种选择,就是在自己的“自定义”记录器中创建一个Singleton,然后您可以从任意位置获取并记录所需内容,将这些值存储在Collection中并从中读取。 –
如果您想保存日志,也可以将它们写入文件。那你绝对不会放弃他们。 –
你可以试试这个在你的设备上查看它http://android-arsenal.com/details/1/2649 –