Reputation: 1060
in my app, i use the AccelerationSensor.accelerationchanged(xAccel, yAccel, zAccel) API the problem is the method is called every o.oooo1 change in any axis, so the app becomes very slow, some times even becomes "non-responding"
Is there a way to check if the integer part has changed and let away any decimal change?
Upvotes: 0
Views: 127
Reputation: 2371
This is what I am doing, in my onStartCommand()
of my service
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
This is the function I am handling my data, it is pretty light weight but it should give you an idea using a threshold. In my case I really just need to know the device a bit, in my case it turned out the differenceValue neeeded to be about 1.75G but it might be different for you.
@Override
public void onSensorChanged(SensorEvent event) {
if(last[0] == 0.0f &&
last[1] == 0.0f &&
last[2] == 0.0f){
last[0] = event.values[0];
last[1] = event.values[1];
last[2] = event.values[2];
return;
}
float diff = 0f;
if(Math.abs(last[0] - event.values[0]) > differenceValue ||
Math.abs(last[1] - event.values[1]) > differenceValue ||
Math.abs(last[2] - event.values[2]) > differenceValue){
Log.d(TAG,"G values are "+event.values[0]+" "+event.values[1]+" "+event.values[2]);
Log.d(TAG,"Last G values are "+last[0]+" "+last[1]+" "+last[2]);
diff = Math.abs(last[0] - event.values[0]);
if(diff < Math.abs(last[1] - event.values[1])){
diff = Math.abs(last[1] - event.values[1]);
}
if(diff < Math.abs(last[2] - event.values[2])){
diff = Math.abs(last[2] - event.values[2]);
}
Log.d(TAG,"Sensor difference: "+diff);
//Do what ever processing you need here
}
last[0] = event.values[0];
last[1] = event.values[1];
last[2] = event.values[2];
}
Upvotes: 1
Reputation: 93708
When you register a listener for a sensor it allows you to set a frequency. Use a slower frequency.
Upvotes: 1