Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onAccuracyChanged, why?

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 ?

like image 462
Motassem MK Avatar asked Sep 20 '15 09:09

Motassem MK


People also ask

What is the purpose of SensorManager?

SensorManager lets you access the device's sensors . Get an instance of this class by calling Context. getSystemService() with the argument SENSOR_SERVICE . Always make sure to disable sensors you don't need, especially when your activity is paused.

What is a SensorEventListener?

SensorEventListener is used for receiving notifications from the SensorManager when sensor values have changed. In this class there are two public methods: onAccuracyChanged(Sensor sensor, int accuracy) : Called when the accuracy of a sensor has changed.

When sensor accuracy is changed what method is called?

void onSensorChanged(SensorEvent event) it is called when sensor values are changed.

What is onSensorChanged?

What is onSensorChanged? [onSensorChanged is] Called when there is a new sensor event. Note that "on changed" is somewhat of a misnomer, as this will also be called if we have a new reading from a sensor with the exact same sensor values (but a newer timestamp).05-Apr-2020.


1 Answers

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.

like image 129
driftking9987 Avatar answered Sep 19 '22 20:09

driftking9987