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) {
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