我要倾倒的Android的logcat中,每当用户想收集日志文件。亚行工具,我们可以使用亚行logcat -f文件名
日志重定向到一个文件,但我怎么能做到这一点编程?
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?
下面是读取日志的例子:http://www.helloandroid.com/tutorials/reading-logs-programatically
here is an example of reading the logs: http://www.helloandroid.com/tutorials/reading-logs-programatically
您可以更改此写入一个文件,而不是到一个TextView。
you could change this to write to a file instead of to a textview.
需要清单的权限:
<uses-permission android:name="android.permission.READ_LOGS" />
code:
Code:
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) {
}
}
}