91-9990449935 0120-4256464 |
Android Sensor TutorialSensors 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 SensorsAndroid supports three types of sensors: 1) Motion SensorsThese are used to measure acceleration forces and rotational forces along with three axes. 2) Position SensorsThese are used to measure the physical position of device. 3) Environmental SensorsThese are used to measure the environmental changes such as temperature, humidity etc. Android Sensor APIAndroid sensor api provides many classes and interface. The important classes and interfaces of sensor api are as follows: 1) SensorManager classThe android.hardware.SensorManager class provides methods :
You can get the instance of SensorManager by calling the method getSystemService() and passing the SENSOR_SERVICE constant in it. 2) Sensor classThe 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 classIts instance is created by the system. It provides information about the sensor. 4) SensorEventListener interfaceIt provides two call back methods to get information when sensor values (x,y and z) change or sensor accuracy changes.
Android simple sensor app exampleLet's see the two sensor examples.
activity_main.xmlThere is only one textview in this file. File: activity_main.xml
Activity classLet's write the code that prints values of x axis, y axis and z axis. File: MainActivity.java
Output:What is next?Click for changing background color of activity sensor example
Next TopicAndroid Simple Graphics Example
|