写的android logcat的数据文件文件、数据、android、logcat

2023-09-11 20:28:09 作者:星辰不辞烟火

我要倾倒的Andr​​oid的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) {
    }
  }
}
 
精彩推荐
图片推荐