我尝试在我的应用程序中读取所有android logcat记录并在列表视图中显示它们。 在我的代码中,有时我可以阅读日志,但有时候不能。 如果我打开logcatactivity我可以读取所有日志,但我无法阅读
oReadLogTask = new ReadLogTask();
oReadLogTask.execute();
private class ReadLogTask extends AsyncTask<Integer, Integer, Boolean>
{
private ProgressDialog oProgressDialog;
@Override
protected void onPreExecute()
{
oLogVeriler = new ArrayList<LogCatVeri>();
oProgressDialog = ProgressDialog.show(LogCat.this, "", "Log loading...");
oProgressDialog.show();
oProgressDialog.setCancelable(true);
oProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
public void onCancel(DialogInterface dialog)
{
oSayfaYukleTask.cancel(true);
}
});
}
protected void onProgressUpdate(Integer... progUpdate)
{
oProgressDialog.setMessage("Log load... " + progUpdate[0] + "/" + progUpdate[1]);
}
@Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
List<String> progs = new ArrayList<String>();
progs.add("logcat");
progs.add("-v");
progs.add("time");
progs.add("*:" + "V");
logcatProc = Runtime.getRuntime().exec(progs.toArray(new String[0]));
mReader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), 1024);
int LineAdetTum = 0;
int LineAdetAranan = 0;
String LogTagAdKucuk = LogTagAd.toLowerCase();
String line;
while ((line = mReader.readLine()) != null)
{
LineAdetTum++;
publishProgress(LineAdetAranan, LineAdetTum);
if (!line.toLowerCase().contains(LogTagAdKucuk))
{
boolean ready = mReader.ready();
if (!ready)
break;
else
continue;
}
else
{
boolean ready = mReader.ready();
if (!ready)
break;
}
LineAdetAranan++;
publishProgress(LineAdetAranan, LineAdetTum);
LogCatVeri oLogVeri = new LogCatVeri(line, LogTagAdKucuk, LogTagAd);
oLogVeriler.add(oLogVeri);
}
return true;
}
catch (IOException ex)
{
Log.e("alogcat", "Log read err" + ex.getMessage(), ex);
return false;
}
catch (Exception ex)
{
Log.e("alogcat", "Log read err2" + ex.getMessage(), ex);
return false;
}
finally
{
if (logcatProc != null)
{
logcatProc.destroy();
logcatProc = null;
}
if (mReader != null)
{
try
{
mReader.close();
mReader = null;
}
catch (IOException e)
{
Log.e("alogcat", "Stream err", e);
}
}
}
}
@Override
protected void onPostExecute(Boolean result)
{
if (result)
ShowRes();
else
Toast.makeText(getApplicationContext(), "Log rec err", Toast.LENGTH_LONG).show();
oProgressDialog.dismiss();
}
@Override
protected void onCancelled(Boolean result)
{
super.onCancelled(result);
Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_LONG).show();
}
}
public void ShowRes()
{
mLogEntryAdapter = new LogCatAdapter(this, R.layout.lc_log_cat_list, oLogVeriler);
mLogList.setAdapter(mLogEntryAdapter);
}
答案 0 :(得分:0)
试试这个。 只需阅读所有日志到现在
Log.e("Log", " 111");
Log.e("Log", "222");
try
{
// read
Process process = Runtime.getRuntime().exec("logcat -d *:e");
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());
// clear
Runtime.getRuntime().exec(new String[]{"logcat", "-c"});
}
catch (IOException e)
{
}