私はAndroidアプリケーションでメッセージをログに記録するためにlogcatを使用しています。これらのメッセージにアクセスしてDialogFragmentのTextViewに表示したいと思います。メッセージのリストは非常に矛盾しており、ダイアログを開いたり閉じたりするたびに変更されます。それは完全な履歴を一度表示し、次に消去するときに表示され、時にはさらにメッセージを表示します。ダイアログを開くたびにすべてのメッセージを表示するために何かできることはありますか?間違った時間などでプロセスを実行していますか?または、バッファを別の場所で処理する必要がありますか?ありがとう。AndroidのTextViewに完全なlogcat履歴を印刷するにはどうすればよいですか?
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();
}
}
は結果を示す 'logTextView'ですか? –
はい、ダイアログを開いて閉じるたびに結果が異なります。 – Brendan
結果は同じではありません、その自然な、ログの猫は違う、冗長を参照してください、あなたは同じを得ることはできません.. –