Refresh View while receiving sensor data
<p>What is the best way to perform a view update while the sensor is giving me some data? Is it possible?</p>

<p>I just want to draw a line that varies its inclination depending on the data received from the sensor.
Is enough the procedure, if I should use threads or not, or if it is necessary to handle my sensor data in another class, etc, thanks again.</p>

<p>this is the custom view code:</p>

<pre><code>import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable.Callback;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class InclinationView extends View implements Callback, SensorEventListener {
    private SensorManager sm;
    private Sensor sensor;
    private float[] mValues;

    private Paint mPaint;
    private Path mPath;
    private int width;
    private int height;

    private Display display;
    private String TAG = "Hud";

    @SuppressWarnings("deprecation")
    public InclinationView(Context context) {
        super(context);
        sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sm.registerListener(InclinationView.this,
                sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_NORMAL);

        display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        String obtainedValues = String.valueOf(event.values[1]);
        Log.d(TAG, obtainedValues + event.toString());

        mValues = event.values;

        if (mValues != null) {
            InclinationView.this.invalidate();
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        mPaint = new Paint();

        mPaint.setAntiAlias(true);
        mPaint.setColor(this.getResources().getColor(R.color.android_blue));
        // canvas.drawLine(width / 2, height / 3, width / 2, (height / 3) * 2,
        // lPaint);
        if (mValues != null) {
            canvas.rotate(mValues[1], width / 2, height / 2);
        }
        canvas.drawLine(width / 3, height / 2, (width / 3) * 2, height / 2, mPaint);
    }

}
</code></pre>

<p>Solved.
It just calls the .invalidate() method each time a SensorEvent occurs.</p>
