本文介绍了将android logcat数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当用户想要收集日志时,我都想将 Android logcat 转储到文件中.通过 adb 工具,我们可以使用 adb logcat -f filename
将日志重定向到文件,但是我如何以编程方式执行此操作?
I want to dump Android logcat in a file whenever user wants to collect logs. Through adb tools we can redirect logs to a file using adb logcat -f filename
, but how can I do this programmatically?
推荐答案
这是一个 示例 阅读日志.
Here is an example of reading the logs.
您可以将其更改为写入文件而不是 TextView
.
You could change this to write to a file instead of to a TextView
.
在AndroidManifest
中需要权限:
<uses-permission android:name="android.permission.READ_LOGS" />
代码:
public class LogTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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) {
log.append(line);
}
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
}
}
这篇关于将android logcat数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!