This tutorial is about programming android hardware sensors service: TYPE_ACCELEROMETER.
You can start by using the 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 reading the values and show us into the text view area.
The SensorEventListener is used to read these values.
The MainActivity will need work with: … implements SensorEventListener.
This example can be used also like a good way to learn about these 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/acceleration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X: Y: Z:" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package org.ft.catafest.accsensordata; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements SensorEventListener{ Sensor accelerometer; SensorManager sensor_manager; TextView acceleration; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensor_manager=(SensorManager)getSystemService(SENSOR_SERVICE); accelerometer = sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensor_manager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); acceleration = (TextView)findViewById(R.id.acceleration); } @Override public void onSensorChanged(SensorEvent event) { acceleration.setText("X: "+event.values[0]+ "\nY: "+event.values[1]+ "\nZ: "+event.values[2]); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } |
The result is a text with the values of the accelerometer of your hardware.