我应该如何登录到 Android 中的 UI?
How should I log to the UI in Android?
我正在尝试找到一种在 Activity 中的 TextView 中显示日志消息的好方法。我在 TextView 中 ,但每次打开和关闭 Fragment 时结果都会有所不同,并且不会显示所有消息。我正在登录多个 Activity 和 classes,所以我试图找到一种记录方式,而不会将其耦合到每个 class。是否有 logcat 之类的替代方法,我可以访问和显示某种类型的系统日志记录?或者有什么有用的库之类的吗?
谢谢
toast 在小弹出窗口中提供有关操作的简单反馈:
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
关于登录 Android 我肯定会推荐:
https://github.com/JakeWharton/timber
例如,您不需要一直做那个烦人的 TAG,因为 Timber 会为您做这件事。
关于你的问题,我认为你可以做些什么来在 Timber 中种植你自己的树。这将确保所有日志记录都通过那里,您可以从那里捕获并记录您想要的一切。请参阅他在应用程序中种植树的示例:https://github.com/JakeWharton/timber/blob/master/timber-sample/src/main/java/com/example/timber/ExampleApp.java
现在这是在应用程序层中,因此您需要以某种方式将它们提取到您的活动中。一个快速的方法,不是很漂亮的方法,我认为你可以做的是将日志存储在一个集合中,并让它静态访问以从应用程序中获取。
附带说明一下,我很好奇您为什么想要这个,为什么 Android Studio 的日志记录工具不够好? :)
我最终将历史保存到一个成员变量中,并且每次打开对话框时只附加新的日志行。
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();
}
}
我正在尝试找到一种在 Activity 中的 TextView 中显示日志消息的好方法。我在 TextView 中
谢谢
toast 在小弹出窗口中提供有关操作的简单反馈:
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
关于登录 Android 我肯定会推荐: https://github.com/JakeWharton/timber
例如,您不需要一直做那个烦人的 TAG,因为 Timber 会为您做这件事。
关于你的问题,我认为你可以做些什么来在 Timber 中种植你自己的树。这将确保所有日志记录都通过那里,您可以从那里捕获并记录您想要的一切。请参阅他在应用程序中种植树的示例:https://github.com/JakeWharton/timber/blob/master/timber-sample/src/main/java/com/example/timber/ExampleApp.java 现在这是在应用程序层中,因此您需要以某种方式将它们提取到您的活动中。一个快速的方法,不是很漂亮的方法,我认为你可以做的是将日志存储在一个集合中,并让它静态访问以从应用程序中获取。
附带说明一下,我很好奇您为什么想要这个,为什么 Android Studio 的日志记录工具不够好? :)
我最终将历史保存到一个成员变量中,并且每次打开对话框时只附加新的日志行。
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();
}
}