How to use onAccuracyChanged function in sensors

不打扰是莪最后的温柔 提交于 2020-01-26 02:54:10

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!