OnSensorChanged Android Accelerometer Information

I am trying to write a game with touch controls on the Android platform. I include the entire motion detection algorithm in the onSensorChanged () function. The problem is that the function is executed at different points in time based on input. I have specific requests about how a function executes and receives a call: -

1) If my function is executed for a long time, when the onSensorChanged event occurs, both are called or only the last ones?

2) Is the onSensorChanged function executed for different threads other than the activity thread?

3) Are multiple instances of the onSensorcChanged function executed for different threads? Is there any type of synchronization for accessing a variable?

4) Can someone point me where I can find detailed information about onSensorChanged () or related information?

5) Is there a way that I can first play the game for a while and see how the values ​​have changed in length and how the function was executed in a different way?

+4
source share
1 answer

It would be more helpful if you could indicate what type of sensor you are using. I assume it is Sensor.TYPE_ORIENTATION .

1) If my function is executed for a long time, when the onSensorChanged event occurs, both are called or only the last ones?

The onSensorChanged method provides an event object ( values parameter) to which the values ​​of a specific sensor are bound. for Sensor.TYPE_ORIENTATION , event.values[0] is the azimuth, event.values[1] is the step, and event.values[2] is Roll. read the SensorEvent page for Android developers to better understand this. The above values ​​are updated pretty quickly, and this is just how you handle these value changes, which determine which value will be used. For example, you can constantly update your view based on changing event values.

2) Is the onSensorChanged function executed for different threads other than the activity thread?

It works in another thread, which depends on the activity to which it was added (an activity that either implemented the SensorEventListener interface or contains an object created from an anonymous inner class created on the SensorEventListener object).

3) Are multiple instances of the onSensorcChanged function executed for different threads? Is there any type of synchronization for accessing a variable?

I'm not too sure about this, but I would suggest that they synchronize the event object during its update. Yes, multiple instances of classes that implement the SensorEventListener interface can be executed separately.

4) Can someone point me where I can find detailed information about onSensorChanged () or related information?

5) Is there a way that I can first play the game for a while and see how the values ​​have changed in length and how the function was executed in a different way?

Do you use graphics or layouts? If you use graphics,

Action 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 code above will display text containing the updated values ​​at the top of the screen. You will notice that these values ​​are updated very often. Similarly, if you are using layouts, just update the text view with the new event values.

Good luck

+6
source

All Articles