Hello coders!
I am experiencing a lot of trouble by trying to process the sensorevents of every sensor in a own thread. These Sensorhandler-threads write the data of one sensorevent into a txt.file and should then await a delay until they react on sensorevents again. According to my knowledge, i could send these threads to sleep, but if i try, the UIThread also freezes. I searched Google for hours and tried to solve it by Handlers but nothing works fine. It seems like the whole code is still processed in a single thread. What am i doing wrong? :-(
The code:
Main(Activity)-class
SensorHandler(Thread)class
I am experiencing a lot of trouble by trying to process the sensorevents of every sensor in a own thread. These Sensorhandler-threads write the data of one sensorevent into a txt.file and should then await a delay until they react on sensorevents again. According to my knowledge, i could send these threads to sleep, but if i try, the UIThread also freezes. I searched Google for hours and tried to solve it by Handlers but nothing works fine. It seems like the whole code is still processed in a single thread. What am i doing wrong? :-(
The code:
Main(Activity)-class
package de.example.fsensor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import de.example.fsensor.R;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static com.google.common.base.Preconditions.*;
@SuppressLint("UseSparseArrays") @TargetApi(Build.VERSION_CODES.KITKAT) public class FSensor extends Activity{
static SensorManager mSensorManager;
private static HashMap<Integer, ArrayList<TextView>> textfields = new HashMap<Integer, ArrayList<TextView>>();
private EditText mEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fsensor);
checkArgument(this.isExternalStorageReadable() == true | this.isExternalStorageWritable() == true, "Externer Speicher kann nicht gelesen bzw. beschrieben werden!");
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mEdit = (EditText)findViewById(R.id.editText1);
initTextfields(textfields);
SharedData.sensorList = initSensorsAndLogs(SharedData.initListLogAndSensor);
startSensorHandlers(SharedData.sensorList);
}
protected void onPause(){
super.onPause();
for (SensorHandler s : SharedData.threadList){
mSensorManager.unregisterListener(s);
s.interrupt();
}
SharedData.threadList.removeAll(SharedData.threadList);
}
protected void onResume(){
super.onResume();
SharedData.sensorList = initSensorsAndLogs(SharedData.initListLogAndSensor);
startSensorHandlers(SharedData.sensorList);
}
public static void inComingSensorchange(SensorEvent event, SensorHandler sensor) {
int type = event.sensor.getType();
textfields.get(type).get(0).setText(String.valueOf(event.values[0]));
textfields.get(type).get(1).setText(String.valueOf(event.values[1]));
textfields.get(type).get(2).setText(String.valueOf(event.values[2]));
}
public void startTracking(View view) {
SharedData.logging = true;
}
public void stopTracking(View view) {
SharedData.logging = false;
}
public void submitDelay(View view) {
SharedData.delay = Integer.parseInt(mEdit.getText().toString());
SharedData.real_delay = SharedData.delay - SharedData.sensorOverhead;
}
// Check if external storage is available for read and write
private boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
// Checks if external storage is available or at least read
private boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
private void initTextfields(HashMap<Integer, ArrayList<TextView>> textfields){
ArrayList<TextView> temp1 = new ArrayList<TextView>();
ArrayList<TextView> temp2 = new ArrayList<TextView>();
ArrayList<TextView> temp3 = new ArrayList<TextView>();
temp1.add((TextView) findViewById(R.id.xboxA));
temp1.add((TextView) findViewById(R.id.yboxA));
temp1.add((TextView) findViewById(R.id.zboxA));
temp2.add((TextView) findViewById(R.id.xboxB));
temp2.add((TextView) findViewById(R.id.yboxB));
temp2.add((TextView) findViewById(R.id.zboxB));
temp3.add((TextView) findViewById(R.id.xboxC));
temp3.add((TextView) findViewById(R.id.yboxC));
temp3.add((TextView) findViewById(R.id.zboxC));
textfields.put(Sensor.TYPE_ACCELEROMETER, temp1);
textfields.put(Sensor.TYPE_MAGNETIC_FIELD, temp2);
textfields.put(Sensor.TYPE_ROTATION_VECTOR, temp3);
}
private HashMap<Sensor, Log> initSensorsAndLogs(Map<Integer, String> init){
HashMap<Sensor, Log> result = new HashMap<Sensor, Log>();
Sensor temp1;
Log temp2;
for(int i : init.keySet()){
temp1 = mSensorManager.getDefaultSensor(i);
temp2 = (new Log(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + init.get(i)));
result.put(temp1, temp2);
}
return result;
}
private void startSensorHandlers(HashMap<Sensor, Log> sensors){
for(Sensor s : SharedData.sensorList.keySet()){
SensorHandler newSensorHandler = new SensorHandler(SharedData.sensorList.get(s));
newSensorHandler.setPriority(Thread.NORM_PRIORITY);
SharedData.threadList.add(newSensorHandler);
mSensorManager.registerListener(newSensorHandler, s, SensorManager.SENSOR_DELAY_FASTEST);
}
}
}
SensorHandler(Thread)class
package de.example.fsensor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
public class SensorHandler extends Thread implements SensorEventListener{
private Log sensorLog;
public SensorHandler(Log sensorLog){
this.sensorLog = sensorLog;
this.start();
}
public void run(){
while(!isInterrupted()){
}
sensorLog.close();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged( final SensorEvent event) {
FSensor.inComingSensorchange(event, this);
if(SharedData.logging){
sensorLog.createNewBlankEntryInFile();
sensorLog.newLogEntry("\n" + "X-Koordinate: " + String.valueOf(event.values[0]) + "\n" + "Y-Koordinate: " + String.valueOf(event.values[1]) + "\n" + "Z-Koordinate: " + String.valueOf(event.values[2]));
sensorLog.createNewBlankEntryInFile();
try {
Thread.sleep(SharedData.delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if(SharedData.real_delay > 0){
// final SensorHandler context = this;
// FSensor.mSensorManager.unregisterListener(this);
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// FSensor.mSensorManager.registerListener(context, event.sensor, SensorManager.SENSOR_DELAY_FASTEST);
// }}, SharedData.real_delay);
// }
}
}
}