我的操作系统是JellyBean。我有一个Android服务,它每5分钟收集一次Logcat日志并保存到txt文件。 但有一个问题。 首先,当我没有任何线程进行编码时,就像那样
@Override
public void onCreate()
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service Üretildi", 1).show();
super.onCreate();
process = Runtime.getRuntime()
.exec("logcat -d -b main -v time");
.
.
.
}
没有问题。应用程序正确收集系统日志。但是当我在TimerTask中使用线程时 它只能记录TestService的调试日志。我该怎么办?
public class TestService extends Service
{
@Override
public void onCreate()
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service Üretildi", 1).show();
super.onCreate();
runService();
}
private void runService()
{
timer.scheduleAtFixedRate(new runningTask(), 0, 300 * 1000);
}
private class runningTask extends TimerTask
{
@Override
public void run()
{
Log.i("LOGTEST", "LOGTEST");
try
{
process = Runtime.getRuntime()
.exec("logcat -d -b main -v time");
}
catch (IOException e)
{
Log.e("LOGTEST", e.getMessage());
}
Log.i("LOGTEST", "COLLECTED");
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
try
{
while ((line = bufferedReader.readLine()) != null)
{
log.append(line);
log.append("\n");
}
// log = stabilizing(log);
bufferedReader.close();
}
catch (IOException e)
{
Log.e("LOGTEST", e.getMessage());
}
String logFilePath = Environment.getExternalStorageDirectory()
+ File.separator + "AAAA" + i + ".txt";
i++;
File logFile = new File(logFilePath);
if (!logFile.exists())
try
{
logFile.createNewFile();
FileOutputStream outStream = new FileOutputStream(logFile,
true);
byte[] buffer = log.toString().getBytes();
outStream.write(buffer);
outStream.close();
}
catch (IOException e)
{
Log.e("LOGTEST", e.getMessage());
}
// Runtime.getRuntime().exec("logcat -c");
finally
{
process.destroy();;
}
}
}
}
答案 0 :(得分:0)
我的理解是,自JellyBean版本(4.2)以来,无法在非root用户设备上执行此操作。如果您设备已root,则应该能够访问系统日志。如果您没有root设备,我想您只能访问应用程序内部生成的日志。