问题
I am making an app in which am using two sensors.
TYPE_MAGNETIC_FIELD
TYPE_GRAVITY
I initialized the respective sensors and then in onSensorChanged
function, am fetching the data and doing the calculations on the same.
I have one simple question, how can I use onAccuracyChanged
function to filter out data? I want the data with medium and high accuracy!!
I printed basic statements to see what kind of accuracy am getting while debugging the app.
Code :
`@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
// You must implement this callback in your code.
// I initialized mValuen as mValuen = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
if (sensor == mValuen) {
switch (accuracy) {
case 0:
System.out.println("Unreliable");
break;
case 1:
System.out.println("Low Accuracy");
break;
case 2:
System.out.println("Medium Accuracy");
break;
case 3:
System.out.println("High Accuracy");
break;
}
}
}`
As per my understanding, whenever a sensor reports a new value onSensorChanged
function is called. So I can't really call that function explicitly(Even if I could,that will anyway be called upon whenever the sensor reports a new value).
All of my calculations are in that function. How do I filter out data with medium and high accuracy. Thanks.
回答1:
Check this answer of mine for more elaborated answer.
Let's say this is the onAccurayChanged
function.
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
// You must implement this callback in your code.
if (sensor == mValuen) {
switch (accuracy) {
case 0:
System.out.println("Unreliable");
con=0;
break;
case 1:
System.out.println("Low Accuracy");
con=0;
break;
case 2:
System.out.println("Medium Accuracy");
con=1;
break;
case 3:
System.out.println("High Accuracy");
con=1;
break;
}
}
}
I declared a global variable and kept it as 0.
And in the onSensorChanged
function, where am doing the required calculations, am putting an if..else
condition. If the con
value is 1, then only am doing the calculations.
Am getting the output. But please let me know if my approach is wrong in some way or other.
Thanks.
来源:https://stackoverflow.com/questions/34273681/how-to-use-onaccuracychanged-function-in-sensors