Android sensor to detect motion

In this Android tip, I am going to show you how to detect motion of Android device. Android provides SensorManager class that can be used to manage all aspects of sensors available on an Android device. Motion sensors are useful to monitor the movements of the device.
To get an instance of SensorManager, you need to pass SENSOR_SERVICE to the getSystemService() method of the current activity. To get a Sensor instance for monitoring the movement of the device, you pass Sensor.TYPE_ACCELEROMETER to the getDefaultSensor() method of the SensorManager instance.
To allow the Sensor instance to monitor the movement of the device, you need to register the SensorEventListener for the sensor. You have to override two methods: onAccuracyChanged and onSensorChanged. In this tip, the onAccuracyChanged method is ignored. We only need to write code in the onSensorChanged() method. The onSensorChanged() method has SensorEvent parameter that contains values array storing movement values on x-axis, y-axis, and z-axis.

Now to have an example on detecting movement of an Android device, you create a new project. Then add a TextView to the activity_main.xml file to display message about the movement.

<LinearLayout xmlns:android=""
   tools:context="com.example.motionsensor.MainActivity" >




In the MainActivity class, you write code to get an instance of SensorManager and an instance of accelerometer sensor, register the SensorEventListener for the sensor, and monitor the movement in onSensorChanged() method as shown below.

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener{
Activity context;
TextView tv;
boolean start=true;
float xLast, yLast;
SensorManager sensorManager;
Sensor sensor;
   protected void onCreate(Bundle savedInstanceState) {
     // Get SensorManager instance
     sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
     // Get ACCELEROMETER sensor
     sensor= sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   protected void onResume(){
   // Register sensor
   sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);
   protected void onPause(){
   // Unregister sensor
   sensorManager.unregisterListener(this, sensor);

   public void onAccuracyChanged(Sensor sensor, int accuracy) {
   public void onSensorChanged(SensorEvent event) {
   //Log.e("Main", "GX="+String.valueOf(event.values[0])+"\nGY="+String.valueOf(event.values[1])+"\nGZ="+String.valueOf(event.values[2]));
   float xCurrent=event.values[0]; // Get current x
   float yCurrent=event.values[1]; // Get current y
   // Initialize last x and y
   // Calculate variation between last x and current x, last y and current y
   float xDelta=xLast-xCurrent;
   float yDelta=yLast-yCurrent;
   if(Math.sqrt(xDelta*xDelta/2)>1) tv.setText("The device is moved horizontally.");
   if(Math.sqrt(yDelta*yDelta/2)>1) tv.setText("The device is moved vertically.");
   // Update last x and y



Posted by: Dara | post date: 12-21-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.