Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Sensor Tutorial


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.

sensor

 

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 methods Description
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.     <TextView
  7.         android:id=“@+id/textView1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“92dp”
  13.         android:layout_marginTop=“114dp”
  14.         android:text=“TextView” />
  15. </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.     SensorEventListener sel = new SensorEventListener(){
  16.         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  17.         public void onSensorChanged(SensorEvent event) {
  18.             float[] values = event.values;
  19.             textView1.setText(“x: “+values[0]+“\ny: “+values[1]+“\nz: “+values[2]);
  20.         }
  21.     };
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         /* Get a SensorManager instance */
  27.         sm = (SensorManager)getSystemService(SENSOR_SERVICE);
  28.         textView1 = (TextView)findViewById(R.id.textView1);
  29.         list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
  30.         if(list.size()>0){
  31.             sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);
  32.         }else{
  33.             Toast.makeText(getBaseContext(), “Error: No Accelerometer.”, Toast.LENGTH_LONG).show();
  34.         }
  35.     }
  36.     @Override
  37.     protected void onStop() {
  38.         if(list.size()>0){
  39.           sm.unregisterListener(sel);
  40.         }
  41.         super.onStop();
  42.     }
  43. }

download this android example

Output:

sensoroutput1

What is next?

Click for changing background color of activity sensor example

Comments

comments