android sensor
DESCRIPTION
National Mobile Application Awareness Development & Capacity Building Program (Android Slides)TRANSCRIPT
![Page 1: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/1.jpg)
Sensor Programming
Android application development
![Page 2: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/2.jpg)
Android Sensors1. Android phones support different sensors. 2. Today we write test code that reports which sensors our Android device supports. 3. Unfortunately, the emulator supports none upto SDK 1.5.4. But we’ll see how many sensors are supported
in real device5. We’ll create a project named SensorTest for this.
We’ll test following sensor list:1. Accelerometer2. Light3. Magnetic Field4. Orientation5. Orientation Raw6. Proximity7. Temperature8. Tricorder This is for SDK 1.5
![Page 3: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/3.jpg)
Let’s create a project named SensorTest
![Page 4: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/4.jpg)
Activity to a ListActivity1. We change our Activity to a ListActivity
![Page 5: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/5.jpg)
Android Sensors Test2. We’ll use SensorManager class3. So we declare a List for SensorInfo 4. SensorInfo is a class defined by us which holds the sensor name and a boolean value whether its supported or not.5. So we create a SensorInfo class
![Page 6: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/6.jpg)
ListActivity6. Let’s get back to our ListActivity
7. We’ll now populate the list with all sensors and check whether they are enabled
![Page 7: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/7.jpg)
Declare a List of SensorInfo8. We declare a List of SensorInfo
9. Now we check each sensor one by one: First Accelerometer
10. Now we check each sensor one by one: then Light
![Page 8: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/8.jpg)
Android Sensors Test11. Now we check each sensor one by one: First Magnetic field
12. Now we check each sensor one by one: then Orientation
![Page 9: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/9.jpg)
Android Sensors Test13. Now we check each sensor one by one: First Orientation Raw
14. Now we check each sensor one by one: then Proximity
![Page 10: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/10.jpg)
Android Sensors Test15. Now we check each sensor one by one: First Temperature
16. Now we check each sensor one by one: then Tricorder
![Page 11: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/11.jpg)
Android Sensors Test17. To show the list we do the following by setting adapter
Now we run the app in emulator.
Except Accelerometer we see all sensors are disabledMay be in later SDKs we’ll get more enabled sensors
![Page 12: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/12.jpg)
Android Sensors TestLets run this in Galaxy tab
We see the following are enabled:
1. Accelerometer2. Light3. Magnetic Field4. Orientation5. Orientation Raw6. Proximity
So the sensor support varies from Vendor to vendor and model to model
![Page 13: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/13.jpg)
Android Sensor Programming
As we see that Accelerometer is the 1. most commonly used sensor and also 2. it has many applications in games development, We’ll see and example of Sensor programming with Accelerometer
And as we don’t have option to move the emulator we may have to use a sensor simulator which will simulate the sensor data.So we download the library for sensorsimulation at:http://openintents.googlecode.com/files/sensorsimulator-1.1.0-rc1.ziphttp://code.google.com/p/openintents/downloads/detail?name=sensorsimulator-1.1.0-rc1.zip&can=2&q=http://code.google.com/p/openintents/wiki/SensorSimulator
We can follow their instruction to use it.
But here, we’ll do it in our real device, so simulator will not be required
![Page 14: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/14.jpg)
Create a project as an exampleWe create a layout with a simple image which will move according to the Accelerometer:
![Page 15: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/15.jpg)
Our Activity
![Page 16: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/16.jpg)
Android Sensor ProgrammingAdding SensorEventListener inner class with data update
![Page 17: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/17.jpg)
Android Sensor ProgrammingDeclaring variables and registering listener
![Page 18: Android Sensor](https://reader035.vdocuments.mx/reader035/viewer/2022062405/557e7a5bd8b42a03668b5272/html5/thumbnails/18.jpg)
Android Sensor ProgrammingRegister and unregister listener on activity onStop and onResume: