5

this is one of the auto-generated functions I get when implementing

SensorEventListener

i've searched and didn't find anymore than this description: "Do something if sensor accuracy changes." when and how does the proximity sensor accuracy changes ? and in what way can this be useful on my apps ?

Motassem Kassab
  • 1,744
  • 4
  • 21
  • 40
  • does it help? http://stackoverflow.com/questions/6038232/how-to-check-accuracy-values-on-onaccuracychanged-method-in-sensor-event-listeni – Marco Acierno Sep 20 '15 at 09:40
  • "The OnAccuracyChanged method of your listener is invoked by system, when a sensor begins to report with different accuracy" yep, that's the answer of part of my question thank you. but still, when does it happen (when does the sensor change its accuracy) and how to make a use of it? – Motassem Kassab Sep 20 '15 at 10:03

1 Answers1

12

According to my understanding, OnAccuracyChanged method is invoked whenever a sensor reports with different accuracy and onSensorChanged method is called whenever a sensor reports a new value.

I too had the same question, as in how to make use of onAccuracyChanged method.

Let's say you want the sensor data only when the accuracy is high,nothing below that. One way to do that is to create a new global variable. Inside the onAccuracyChanged method, initialize it to some value, and inside onSensorChanged function, add an if..else condition.

Sample code:

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=0;

                    break;
                case 3:
                    System.out.println("High Accuracy");
                    con=1;
                    break;
            }
        }
    }

Now use condition as if (con==1) to do the required changes.

P.S. According to the android documentation.

public abstract void onAccuracyChanged (int sensor, int accuracy)

Called when the accuracy of a sensor has changed. See SensorManager for details.Parameterssensor The ID of the sensor being monitored accuracy The new accuracy of this sensor. So whenever the accuracy changes,you will have a reference to that change. Please let me know if am missing something. Thanks.

KUSHA B K
  • 1,489
  • 13
  • 20
driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • 1
    What is the value of mValuen? – Shivam Kumar May 28 '18 at 07:01
  • 2
    `mValuen` will be the type of sensor which you will be initialising. For example, `mValuen = sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED);` and then `sManager.registerListener(this, mValuen, SensorManager.SENSOR_DELAY_UI);` – driftking9987 May 28 '18 at 07:11