0% found this document useful (0 votes)
8 views

Sensor

The document discusses Android sensors and provides details about the different types of sensors, Android sensor API and an example to list available sensors. It explains that there are three types of sensors - motion, position and environmental sensors. It also describes the important classes and interfaces of the Android sensor API like SensorManager, Sensor and SensorEventListener.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Sensor

The document discusses Android sensors and provides details about the different types of sensors, Android sensor API and an example to list available sensors. It explains that there are three types of sensors - motion, position and environmental sensors. It also describes the important classes and interfaces of the Android sensor API like SensorManager, Sensor and SensorEventListener.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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:
o to get sensor instance,
o to access and list sensors,
o 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.

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 it is called when sensor accuracy is


sensor, int accuracy) changed.

void onSensorChanged(SensorEvent it is called when sensor values are


event) changed.

Android Sensors Example


Following is the example of identifying the sensors and list all the available sensors on a device using
android sensor framework.

Create a new android application using android studio and give names as SensorExample. In case if
you are not aware of creating an app in android studio check this article Android Hello World App.

Once we create an application, open activity_main.xml file from \res\layout folder path and write
the code like as shown below.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="@+id/sensorslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Sensors"
android:textSize="20dp"
android:textStyle="bold"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>

MainActivity.java
package com.tutlane.sensorsexample;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.List;

public class MainActivity extends AppCompatActivity {


private SensorManager mgr;
private TextView txtList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mgr = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
txtList = (TextView)findViewById(R.id.sensorslist);
List<Sensor> sensorList = mgr.getSensorList(Sensor.TYPE_ALL);
StringBuilder strBuilder = new StringBuilder();
for(Sensor s: sensorList){
strBuilder.append(s.getName()+"\n");
}
txtList.setVisibility(View.VISIBLE);
txtList.setText(strBuilder);
}
}

You might also like