How to use onAccuracyChanged function in sensors

时间:2015-12-14 18:04:29

标签: android sensor android-sensors

I am making an app in which am using two sensors.

  1. TYPE_MAGNETIC_FIELD
  2. 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 个答案:

答案 0 :(得分:0)

检查我的this答案以获得更详细的答案。

假设这是onAccurayChanged函数。

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;
            }
        }
    }

我声明了一个全局变量并将其保持为0。 在onSensorChanged函数中,我正在进行必要的计算,我正在设置if..else条件。如果con值为1,则仅进行计算。 我得到了输出。但是,如果我的方法在某种程度上是错误的,请告诉我。 感谢。