My OS is JellyBean.I have an android service that makes collecting Logcat logs and saving to txt file every 5 minutes. But there is a problem. First when i was coding without any threads.like that
@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");
.
.
.
}
There was no problem.Application collects system logs properly.But when i used thread in TimerTask It can logging only TestService's debug logs.What should i do?
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();;
}
}
}
}