Android Sensor Tutorial


Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. These sensors are capable of providing raw data with high precision and accuracy, and are useful if you want to monitor three-dimensional device movement or positioning, or you want to monitor changes in the ambient environment near a device. For example, a game might track readings from a device’s gravity sensor to infer complex user gestures and motions, such as tilt, shake, rotation, or swing. Likewise, a weather application might use a device’s temperature sensor and humidity sensor to calculate and report the dewpoint, or a travel application might use the geomagnetic field sensor and accelerometer to report a compass bearing.

The Android platform supports three broad categories of sensors:

  • Motion sensors

    These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.

  • Environmental sensors

    These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.

  • Position sensors

    These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers. You can access sensors available on the device and acquire raw sensor data by using the Android sensor framework. The sensor framework provides several classes and interfaces that help you perform a wide variety of sensor-related tasks.

One of the most interesting topics in my opinion is how to
use Sensor in Android. Nowadays our smartphone are full of sensors and we can use it to control somehow our app. The most common sensors are:

  • GPS

  • 1. Proximity sensor
  • 2. Light sensor
  • 3. Temperature sensor
  • 4. Barometer sensor
  • 5. NFC

just to mention some of them. In this post we will explain how to obtain a list of sensor

We can retrieve the sensor list too and we need a bit of code.

Some of the sensors are hardware based and some are software based sensors. Whatever the sensor is , android allows us to get the raw data from these sensors and use it in our application. For this android provides us with some classes.

Android provides SensorManager and Sensor classes to use the sensors in our application. In order to use sensors , first thing you need to do is to instantiate the object of SensorManager class. It can be achieved as follows.

SensorManager sMgr; sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);

The next thing you need to do is to instantiate the object of Sensor class by calling the getDefaultSensor() method of the SensorManager class. Its sytanx is given below

Sensor light; light = sMgr.getDefaultSensor(Sensor.TYPE_LIGHT);

Once that sensor is declared , you need to register its listener and override two methods which are onAccuracyChanged and onSensorChanged. Its syntax is as follows

 sMgr.registerListener(this, light,SensorManager.SENSOR_DELAY_NORMAL); public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { } 

Getting list of sensors supported.

You can get a list of sensors supported by your device by calling the getSensorList method , which will return a list of sensors containing their name and version number and much more information. You can then iterate the list to get the information. Its syntax is given below:

sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE); List<Sensor> list = sMgr.getSensorList(Sensor.TYPE_ALL); for(Sensor sensor: list){ }


package com.example.sensors; import java.util.List; import; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private SensorManager sMgr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView sensorsData = (TextView)findViewById(; sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE); List list = sMgr.getSensorList(Sensor.TYPE_ALL); StringBuilder data = new StringBuilder(); for(Sensor sensor: list){ data.append(sensor.getName() + "\n"); data.append(sensor.getVendor() + "\n"); data.append(sensor.getVersion() + "\n"); } sensorsData.setText(data); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } }


<RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </ScrollView> </RelativeLayout> 


< ?xml version="1.0" encoding="utf-8"?> Sensors Settings Hello world! List of sensors supported


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="" package="com.example.sensors" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sensors.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Let’s try to run our Sensor application we just modified. I assume you had created your
AVD while doing environment setup. To run the app from Eclipse, open one of your project’s activity files and click

Android Sensor Tutorial

Posted in All Tutorials, Android Tutorials | Tagged | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered By: