【发布时间】:2019-03-15 03:30:24
【问题描述】:
连续读取logcat并写入内部存储我尝试了下面的代码。
public class LogCatTask extends AsyncTask<Void, String, Void> {
public AtomicBoolean run = new AtomicBoolean(true);
@Override
protected Void doInBackground(Void... params) {
try {
//create text file in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");
Runtime.getRuntime().exec("logcat -c");
Process process = Runtime.getRuntime().exec("logcat -f "+file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line = "";
Log.e("log cat task...","while...run.get()."+run.get());
while (run.get()) {
line = bufferedReader.readLine();
//Log.e("log cat task...","while...out.");
if (line != null) {
Log.e("log cat task...","while....");
log.append(line);
//publishProgress(log.toString());
//to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(log.toString());
osw.flush();
osw.close();
}
line = null;
Thread.sleep(10);
//Log.e("log cat task...","while...out.");
}
//Log.e("log cat task...","ouet....while....");
}
catch(Exception ex){
}
return null;
}
}
一旦上面的代码运行,它会读取 logcat 并写入存储,但最多只能读取日志的某些部分。它不读取 continuos。如何连续读取 logcat?
【问题讨论】:
-
你能找到解决办法吗?