Android Sensor

Describe sensors in Android.

Sensors
Sensors are objects or devices that detect events and respond according to input and events.

The Android framework provides the three types of sensors:

Motion sensors

Accelerometers, gravity sensors, gyroscopes, and rotational vector sensors are motion sensors and used to measure acceleration forces and rotational forces along three axes.

Environmental sensors

Barometers, photometers, and thermometers are Environmental sensors. These sensors can be used to measure temperature and pressure, illumination, and humidity etc.

Position sensors

Orientation sensors and magnetometers are position sensors. These sensors are used to measure the physical position of a mobile device or tablet. Some of the sensors are hardware-based and some are software-based. Hardware-based sensors are real devices, those are built into your mobile device or tablet. Software-based sensors work along with hardware-based sensors.

Some of the important sensor types supported by the Android platform are as follows.
  • TYPE_ACCELEROMETER
  • TYPE_GRAVITY
  • TYPE_LIGHT
  • TYPE_ORIENTATION
  • TYPE_PRESSURE
  • TYPE_TEMPERATURE
  • TYPE_RELATIVE_HUMIDITY
Android framework provides many classes and interface for sensor programming. The important classes and interfaces of sensor are as follows:

SensorManager class
SensorManager class is most important class in android regarding sensor. It enables you to access the device's sensors. You can initialize this class as follows.
SensorManagersensormanager = (SensorManager)getSystemService(SENSOR_SERVICE);

Sensor class
This class provides different methods to get information about the sensor such as sensor name, sensor type, sensor resolution etc.

SensorEventListener interface
After getting a sensor, you can register a SensorEventListener object on it. This listener will get informed, if the sensor data changes.

This class has two important methods.
  • void onAccuracyChanged(Sensor sensor, int accuracy):
  • This method is calledwhen sensor accuracy is changed.
  • onSensorChanged(SensorEvent event):
  • This method is called when sensor values are changed.

Create an android application that will give you the name of sensors, available in your device.

Following is the content of res/layout/activity_main.xml file:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/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">

<ListView
android:id = "@+id/listView1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentTop = "true">
</ListView>
</RelativeLayout>

Following is the content of the modified MainActivity.java file
publicclassMainActivityextends Activity
{
     SensorManagersensormanager;
     ListViewlv;
     @Override
     protectedvoidonCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          lv = (ListView)findViewById(R.id.listView1);
          sensormanager = (SensorManager)getSystemService (SENSOR_SERVICE);
          List<Sensor > list = sensormanager.getSensorList (Sensor.TYPE_ALL);
          ArrayAdapter<Sensor > ad = newArrayAdapter<Sensor>(this, android.R.layout.simple_dropdown_item_1line, list);
          lv.setAdapter(ad);
     }
     @Override
     publicbooleanonCreateOptionsMenu(Menu menu)
     {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          returntrue;
     }
}

Sensor

Please note that output will differ according to your emulator type.

Give one example of ACCELEROMETER Sensor.

Following is the content of res/layout/activity_main.xml file:
<RelativeLayoutxmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/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"
>

<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/hello_world"
android:id = "@+id/textView1"
/>

</RelativeLayout>

Following is the content of the modified MainActivity.java file
publicclassMainActivityextends Activity implementsSensorEventListener
{
     SensorManagersm;
     Sensor sensor;
     TextViewtext;
     @Override
     protectedvoidonCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          text = (TextView)findViewById(R.id.textView1);
          layout = (RelativeLayout)findViewById(R.id.RL);
          sm = (SensorManager)getSystemService(SENSOR_SERVICE);
          sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
     }
     @Override
     publicvoidonAccuracyChanged(Sensor sensor, int accuracy)
     {
          // TODO Auto-generated method stub
     }
     @Override
     publicvoidonSensorChanged(SensorEvent event)
     {
          // TODO Auto-generated method stub
          float x = event.values[0];
          float y = event.values[1];
          float z = event.values[2];
          text.setText("X = "+x+"y = "+y+"z = "+z);
     }
}

Note: Please run this application on your Android mobile, not on emulator. If you run this application on emulator, then you will not get any result.