How to intercept tilting in Android?

1.7k views Asked by At

I would like to write a program using tilting functionality in android.

Is there any way to intercept it? What do I get back? a vector indicating the direction of the tilt?

2

There are 2 answers

0
Tim On BEST ANSWER

The main thing to get your head around it the concept of a listener. In Android there isn't a method called getXtilt(), getYtilt() etc to get the orientation. Instead you need to create a listener which you register with the system.

Look at this.

See the onSensorChanged(SensorEvent event) method? The Android system will invoke that method every time the sensor changes (which is very frequently). In this case it will be the TYPE_ACCELEROMETER sensor readings you will be receiving.

So when you get the SensorEvent 'event' have a look at the event.values[] array. It will contain the sensor readings. In the example code in the Android doc they register the Sensor.TYPE_ACCELEROMETER. You should register the Sensor.TYPE_ORIENTATION. Have a look at the values array for Sensor.TYPE_ORIENTATION. They are the tilt values you are looking for.

hope that helps

0
JoxTraex On

Sounds like you would have to use the Accelerometer and interpret the values and then make a decision based on them. Look up SensorEventListener and it should get you in the right direction.