Fundamental components in Android: Accelerometer

- Andrés Cruz

En español

Fundamental components in Android: Accelerometer

Code

In this entry we will see one of the fundamental components that we can find in any relatively recent Android device and it is a sensor that allows us to measure the acceleration of the device: the accelerometer.

The accelerometer in Android is measured based on the three known axes (X,Y and Z); each of them can be accessed through the SensorManager class.

Getting started with the SensorManager class in Android

We must implement the SensorEventListener class in our activity:

public class MainActivity extends Activity implements SensorEventListener

Once we implement the previous interface we must overload the methods:

@Override public void onSensorChanged(SensorEvent event) { } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { }

Requesting access to the accelerometer

The next thing we must do is request access to the accelerometer of the device; and we do this through the SensorManager object as follows:

SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

We obtain the system service through the getSystemService object, passing as a parameter the name of the service we want to access.

Getting system accelerometer

From the list of lap sensors in the previous query, we check if there are any accelerometer-type sensors available; to do this, the following line of code is used:

sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size()

Registering the "Listener" event or Accelerometer Listener

Once we have verified that the device has at least one accelerometer-type sensor, we can register the listener event:

sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST)

We pass as a parameter an instance of the activity, the accelerometer and the refresh rate, which can be:

  • SENSOR_DELAY_FASTEST
  • SENSOR_DELAY_NORMAL
  • SENSOR_DELAY_GAME
  • SENSOR_DELAY_UI

The only thing that varies between them is the refresh rate of the accelerometer.

Registering the "Escudador" or Listener event

The onSensorChanged method is called whenever the system detects a change in device acceleration:

@Override public void onSensorChanged(SensorEvent event) { sensor = ""; sensor = "x: " + event.values[0] + " y: " + event.values[1] + " z: " + event.values[2]; }

With the SensorEvent parameter we can have, among various data, the coordinates that the system uses in the rotations through the X, Y and Z axes as we saw in the previous code.

You can find a working application that exemplifies the behavior of the codes seen above in our Git repository:

Code

App Screenshot:

Acelerómetro en AnDroid
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 2d 06:09!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!