AdSense

AdSense3

Tuesday, 3 November 2015

Android Sensor

Sensors can be used to monitor the three-dimensional device movement or change in the environment of the device.

Android provides sensor api to work with different types of sensors.
Types of Sensors
Android supports three types of sensors:

1) Motion Sensors

These are used to measure acceleration forces and rotational forces along with three axes.

2) Position Sensors

These are used to measure the physical position of device.

3) Environmental Sensors

These are used to measure the environmental changes such as temperature, humidity etc.

Android Sensor API

Android sensor api provides many classes and interface. The important classes and interfaces of sensor api are as follows:

1) SensorManager class

The android.hardware.SensorManager class provides methods :
  • to get sensor instance,
  • to access and list sensors,
  • to register and unregister sensor listeners etc.
You can get the instance of SensorManager by calling the method getSystemService() and passing the SENSOR_SERVICE constant in it.
  1. SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);  

2) Sensor class

The android.hardware.Sensor class provides methods to get information of the sensor such as sensor name, sensor type, sensor resolution, sensor type etc.

3) SensorEvent class

Its instance is created by the system. It provides information about the sensor.

4) SensorEventListener interface

It provides two call back methods to get information when sensor values (x,y and z) change or sensor accuracy changes.
Public and abstract methodsDescription
void onAccuracyChanged(Sensor sensor, int accuracy)it is called when sensor accuracy is changed.
void onSensorChanged(SensorEvent event)it is called when sensor values are changed.

Android simple sensor app example

Let's see the two sensor examples.
  1. A sensor example that prints x, y and z axis values. Here, we are going to see that.
  2. A sensor example that changes the background color when device is shuffled. Click for changing background color of activity sensor example

activity_main.xml

There is only one textview in this file.
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="92dp"  
  14.         android:layout_marginTop="114dp"  
  15.         android:text="TextView" />  
  16.   
  17. </RelativeLayout>  

Activity class

Let's write the code that prints values of x axis, y axis and z axis.
File: MainActivity.java
  1. package com.example.sensorsimple;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5. import android.widget.Toast;  
  6. import android.hardware.SensorManager;  
  7. import android.hardware.SensorEventListener;  
  8. import android.hardware.SensorEvent;  
  9. import android.hardware.Sensor;  
  10. import java.util.List;  
  11. public class MainActivity extends Activity {  
  12.     SensorManager sm = null;  
  13.     TextView textView1 = null;  
  14.     List list;  
  15.   
  16.     SensorEventListener sel = new SensorEventListener(){  
  17.         public void onAccuracyChanged(Sensor sensor, int accuracy) {}  
  18.         public void onSensorChanged(SensorEvent event) {  
  19.             float[] values = event.values;  
  20.             textView1.setText("x: "+values[0]+"\ny: "+values[1]+"\nz: "+values[2]);  
  21.         }  
  22.     };  
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.   
  29.         /* Get a SensorManager instance */  
  30.         sm = (SensorManager)getSystemService(SENSOR_SERVICE);  
  31.   
  32.         textView1 = (TextView)findViewById(R.id.textView1);  
  33.   
  34.         list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);  
  35.         if(list.size()>0){  
  36.             sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);  
  37.         }else{  
  38.             Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG).show();  
  39.         }  
  40.     }  
  41.   
  42.     @Override  
  43.     protected void onStop() {  
  44.         if(list.size()>0){  
  45.           sm.unregisterListener(sel);  
  46.         }  
  47.         super.onStop();  
  48.     }  
  49. }  

Output:

android sensor example output 1

No comments:

Post a Comment