Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Info on onSensorChanged of android acccelerometer

Tags:

android

I am trying to write a motion sensing game on the android platform. I am including my whole algorithm of motion detection into the onSensorChanged() function. The problem is that the function executes for different times based on the input. I have certain queries about how the function executes and gets called:-

1) If my function executes for a long time that 2 onSensorChanged event occurs do both get called or only the latest one?

2) Does the onSensorChanged function run on different threads other than the activity thread?

3) Do multiple instance of onSensorcChanged function run on different threads? Is there any type of synchronization on the variable access?

4) Can anyone point me to where i can find any detail information about onSensorChanged() or related information?

5) Is there any way i can first play the game for sometime and see how the values changed over tine and how the function executed differently?

like image 898
Bk Baba Avatar asked Apr 21 '11 11:04

Bk Baba


1 Answers

it would be more useful if you could provide which type of sensor you're using. I'm going to assume it's Sensor.TYPE_ORIENTATION.

1) If my function executes for a long time that 2 onSensorChanged event occurs do both get called or only the latest one?

The onSensorChanged method provides an event object (values parameter), to which the values of the specific sensor are attached. for Sensor.TYPE_ORIENTATION, event.values[0] is the Azimuth, event.values[1] is the Pitch, and event.values[2] is the Roll. Read the Android Developers SensorEvent page to understand this better. The above values are updated quite quickly, and it is only the way you handle these value changes that determines which value update will be used. For example, you could continually update your view based on how the event values are changed.

2) Does the onSensorChanged function run on different threads other than the activity thread?

It runs on a different thread which is dependent on the activity it has been appended to (the activity that either implemented the SensorEventListener interface or contains an object instantiated from an anonymous inner class created on the SensorEventListener object).

3) Do multiple instance of onSensorcChanged function run on different threads? Is there any type of synchronization on the variable access?

I'm not too sure about this, but I would imagine they would synchronize the event object while it is being updated. Yes, multiple instances of classes that implement the SensorEventListener interface can run separately.

4) Can anyone point me to where i can find any detail information about onSensorChanged() or related information?

  • SensorEventListener documentation

5) Is there any way i can first play the game for sometime and see how the values changed over tine and how the function executed differently?

Are you using graphics or layouts? If you're using graphics,

Activity class:

public class YourActivity extends Activity {
  MyView _view;
  int sensorAccuracy;
  SensorManager sensorManager;
  SensorEventListener sensorListener = new SensorEventListener() {
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
      sensorAccuracy = accuracy;
    }
    public void onSensorChanged(SensorEvent event) {
      //pass the values to view for display
      _view.setOrientation(event.values[0],event.values[1],event.values[2]);
    }
  };
  protected void onCreate(Bundle savedInstanceState) {
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(sensorListener,
            sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_FASTEST);
    _view = new MyView(this);
  }
}

View Class

class MyView extends View() {
         Context _context;
         int _azimuth, _pitch, _roll;
         public MyView(Context context) {
          super(context);
          _context = context;
         }
         public setOrientation(azimuth, pitch, roll) {
          _azimuth = azimuth;
          _pitch = pitch;
          _roll = roll;
        }
        protected void onDraw(Canvas canvas) {
         paint.setStyle(Paint.Style.FILL);
         paint.setColor(Color.BLACK);
         canvas.drawText("Azimuth: " + _azimuth + "Pitch: " + _pitch + "Roll: "
                    + _roll, 10, 10, paint);
        }
      }

The above code will display the text containing the updated values at the top of the screen. You will notice that these values are updated very frequently. Similarly, if you're using layouts, simply update a text view with the new event values.

Good Luck!

like image 102
ramdesh Avatar answered Sep 28 '22 04:09

ramdesh