OK I figured out the problem.
The first problem was that I started the mQueueThread
inside the onSensorChanged
method. This caused a new thread started on receiving update from the onSensorChanged
method and cause meamory leak or some other serious problems. So I moved it to the constructor.
Secondly I changed the Queue type to ConcurrentLinkedQueue
but I dont have any explanation...might be another type would do better.
Lastly, I changed the implementation of Runnable
to the following:
@Override
public void run() {
while (true) {
if (mLogOutStream != null && !mQueue.isEmpty()) {
try {
mLogOutStream.write(mQueue.poll().getDataStringLine().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
if (mLogFile.length() > AppConstants.LOG_FILE_MAX_SIZE) {
synchronized (this) {
reportCurrentLogFileIsFinished(mLogFile);
try {
if (mLogOutStream != null) {
mLogOutStream.flush();
mLogOutStream.close();
mLogOutStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
mLogFile = createLogFile();
mLogOutStream = new FileOutputStream(mLogFile, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
}
These changes took care of the problems. Hope this can help others too.