Game Development Reference
In-Depth Information
So how do we get that accelerometer information? You guessed correctly—by registering
a listener. The interface we need to implement is called SensorEventListener , which has
two methods:
public void onSensorChanged(SensorEvent event);
public void onAccuracyChanged(Sensor sensor, int accuracy);
The first method is called when a new accelerometer event arrives. The second method is called
when the accuracy of the accelerometer changes. We can safely ignore the second method for
our purposes.
So where do we register our SensorEventListener ? For this, we have to do a little bit of work.
First, we need to check whether there actually is an accelerometer installed in the device. Now,
we just told you that all Android devices must contain an accelerometer. This is still true, but it
might change in the future. We therefore want to make 100 percent sure that that input method
is available to us.
The first thing we need to do is get an instance of the SensorManager . That guy will tell us
whether an accelerometer is installed, and it is also where we register our listener. To get the
SensorManager , we use a method of the Context interface:
SensorManager manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
The SensorManager is a system service that is provided by the Android system. Android is
composed of multiple system services, each serving different pieces of system information to
anyone who asks nicely.
Once we have the SensorManager , we can check whether the accelerometer is available:
boolean hasAccel = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0;
With this bit of code, we poll the SensorManager for all the installed sensors that have the type
accelerometer . While this implies that a device can have multiple accelerometers, in reality this
will only ever return one accelerometer sensor.
If an accelerometer is installed, we can fetch it from the SensorManager and register the
SensorEventListener with it as follows:
Sensor sensor = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
boolean success = manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
The argument SensorManager.SENSOR_DELAY_GAME specifies how often the listener should be
updated with the latest state of the accelerometer. This is a special constant that is specifically
designed for games, so we happily use that. Notice that the SensorManager.registerListener()
method returns a Boolean, indicating whether the registration process worked or not. That
means we have to check the Boolean afterward to make sure we'll actually get any events from
the sensor.
Once we have registered the listener, we'll receive SensorEvent s in the
SensorEventListener.onSensorChanged() method. The method name implies that it is only called
when the sensor state has changed. This is a little bit confusing, since the accelerometer state
is changed constantly. When we register the listener, we actually specify the desired frequency
with which we want to receive our sensor state updates.
 
Search WWH ::




Custom Search