Code example for AtomicBoolean

Methods: compareAndSet, set

0
     
    //WHEN THERES A DATA FROM THE SENSOR CHANGE COMPUTING TO TRUE 
    //AND LOOK AT THE CURRENT VS. PREVIOUS VALUES. 
    @Override 
    public void onSensorChanged(SensorEvent evt) {
        if (!computing.compareAndSet(false, true)) return;
 
        if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            grav[0] = evt.values[0];
            grav[1] = evt.values[1];
            grav[2] = evt.values[2];
        } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            mag[0] = evt.values[0];
            mag[1] = evt.values[1];
            mag[2] = evt.values[2];
        } 
 
        float gravity = grav[0] + grav[1] + grav[2];
        float magnetic = mag[0] + mag[1] + mag[2];
 
        float gravDiff = Math.abs(gravity - prevGrav);
        float magDiff = Math.abs(magnetic - prevMag);
        Log.i(TAG, "gravDiff="+gravDiff+" magDiff="+magDiff);
 
        if ((Float.compare(prevGrav, 0.0f) != 0 && Float.compare(prevMag, 0.0f) != 0) && (gravDiff > gravThreshold || magDiff > magThreshold)) {
            GlobalData.setPhoneInMotion(true); 
        } else { 
            GlobalData.setPhoneInMotion(false); 
        } 
 
        prevGrav = gravity;
        prevMag = magnetic;
 
        computing.set(false);
    } 
 
    //How does this onAccuracy change work? - Shawn 
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) {