Android Studio – using SensorEventListener.

By | 13/04/2018

This tutorial is about programming android hardware sensors service: TYPE_ACCELEROMETER.
You can start by using last version of Android Studio version 3.1.1 with the default project Empty Activity.
Is very simple to use accelerometer with Android Studio by read the values and show us into the text view area.
The SensorEventListener is used to read this values.
The MainActivity will need work with: … implements SensorEventListener.
This example can be used also like a good way to learn about this issues.
Note:

  • the implements is a keyword specific to interfaces, whereas extends is used to inherit classes;
  •  a class must implement all methods of an interface, unless the class is declared as abstract.

You need to change the default text Hello World from activity_main.xml file with this:

Make changes into MainActivity.java file
The functions: onSensorChanged and onAccuracyChanged is not created by I.D.E. and can be created by code inspections when you write the source code.

The result is a text with the values of accelerometer of your hardware.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.