Issue
I have wrote a service to log accelerometer values in background. The idea is to register for sensor listener and write the values to storage. I have enabled the StrictMode.enableDefaults()
in the MainActivity
of the app (not in the service) to check if the service runs on its own thread. But it does not! I don't know what is the problem...I have this AsyncTask
class that I hoped to take care of the problem but it apparently did not (this is inside the service and its execute method is called from the onCreate
method of the service.
private class LogTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
mListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
String formatted = String.valueOf(System.currentTimeMillis())
+ "\t" + String.valueOf(sensorEvent.timestamp)
+ "\t" + String.valueOf(sensorEvent.values[0])
+ "\t" + String.valueOf(sensorEvent.values[1])
+ "\t" + String.valueOf(sensorEvent.values[2])
+ "\r\n";
//if (mIsServiceStarted && mFileStream != null && mLogFile.exists()) {
if (mFileStream != null && mLogFile.exists()) {
try {
mFileStream.write(formatted.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mSensorManager.registerListener(
mListener, mSensor, SensorManager.SENSOR_DELAY_FASTEST
);
return null;
}
}
StrictMode in log cat specially points that the following line is voiding the policy:
mFileStream.write(formatted.getBytes());
StrictMode policy violation; ~duration=0 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=31 violation=1
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1092)
Can someone tell me what is going wrong? Here is the full code of my service if requried: http://pastebin.com/NRm376BL
Solution
I found out that the problem is, I should create a HandlerThread
and a Handler
and pass the handler to an overload of registerListener
method. If you dont provide this argument the listener will be handled by the main thread and most of the time this isn't what you are looking for.
For an example implementation check this code: particularly for handling sensor listener on another thread not by main thread
Answered By - Dumbo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.