Android 1.8 sensor
-
Upload
ahsanul-karim -
Category
Technology
-
view
1.253 -
download
0
description
Transcript of Android 1.8 sensor
Android Application DevelopmentSensor Programming
Ahsanul Karim [email protected]
Sentinel Solutions Ltd.http://www.sentinelbd.com
Sensor ProgrammingAndroid Sensors
1. 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
Sensor ProgrammingAndroid Sensors Test
We create a project named SensorTest
Sensor ProgrammingAndroid Sensors Test
1. We change our Activity to a ListActivity
Sensor ProgrammingAndroid Sensors Test
2. 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
Sensor ProgrammingAndroid Sensors Test
6. Let’s get back to our ListActivity
7. We’ll now populate the list with all sensors and check whether they are enabled
Sensor ProgrammingAndroid Sensors Test
8. 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
Sensor ProgrammingAndroid Sensors Test
11. Now we check each sensor one by one: First Magnetic field
12. Now we check each sensor one by one: then Orientation
Sensor ProgrammingAndroid Sensors Test
13. Now we check each sensor one by one: First Orientation Raw
14. Now we check each sensor one by one: then Proximity
Sensor ProgrammingAndroid Sensors Test
15. Now we check each sensor one by one: First Temperature
16. Now we check each sensor one by one: then Tricorder
Sensor ProgrammingAndroid Sensors Test
17. 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
Sensor ProgrammingAndroid Sensors Test
Lets 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
Sensor ProgrammingAndroid 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
Sensor ProgrammingAndroid Sensor Programming
Let’s create a project as an example We create a layout with a simple image which will move according to the Accelerometer:
Sensor ProgrammingAndroid Sensor Programming
Our Activity:
Sensor ProgrammingAndroid Sensor Programming
Adding SensorEventListener inner class with data update
Sensor ProgrammingAndroid Sensor Programming
Declaring variables and registering listener
Sensor ProgrammingAndroid Sensor Programming
Register and unregister listener on activity onStop and onResume: