I am trying to send data of the magnetic field sensor to the Server every 5 seconds but I am facing Problem to unregister the sensorListerner from reading data. I have tried it like this sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),1000 * 10);
too but without seccuss. Currently the data are being sent to the Server 12 time in one second.
I appreciate any help.
Code:
public class MainActivity extends ActionBarActivity implements
SensorEventListener {
String telsaString;
private TextView magneticX;
private TextView magneticY;
private TextView magneticZ;
private TextView magneticT;
private SensorManager sensorManager = null;
private float magX;
private float magY;
private float magZ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Capture magnetic sensor related view elements
magneticX = (TextView) findViewById(R.id.valMag_X);
magneticY = (TextView) findViewById(R.id.valMag_Y);
magneticZ = (TextView) findViewById(R.id.valMag_Z);
magneticT = (TextView) findViewById(R.id.valMag_T);
// Register magnetic sensor
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
final SensorEventListener listener = this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sensorManager.unregisterListener(listener);
}
}, 5000);
}
@Override
protected void onPause() {
// Unregister the listener
sensorManager.unregisterListener(this);
super.onPause();
}
@Override
protected void onStop() {
// Unregister the listener
sensorManager.unregisterListener(this);
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magX = sensorEvent.values[0];
magY = sensorEvent.values[1];
magZ = sensorEvent.values[2];
magneticX.setText(Float.toString(sensorEvent.values[0]));
magneticY.setText(Float.toString(sensorEvent.values[1]));
magneticZ.setText(Float.toString(sensorEvent.values[2]));
double teslaXYZ = (Math.sqrt((magX * magX) + (magY * magY)
+ (magZ * magZ)));
magneticT.setText(Double.toString(teslaXYZ));
try {
JSONObject tesla = new JSONObject();
tesla.put("tesla", teslaXYZ);
telsaString = tesla.toString();
new MyAsyncTask().execute(telsaString);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Edit:
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
10*1000*1000);
.
.
.
.
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 10000) {
lastUpdate = curTime;
}
The rate you provided
1000 * 10
is in microseconds, so you are requesting a 10 millisecond update period. Note that the requested sample period is only a request. The rate may be faster or slower, you need to track the times and do the appropriate thing. When you wish to completely stop receiving events, unregister your listener with theSensorManager.unregisterListener()
method.