SlideShare a Scribd company logo
1 of 18
Download to read offline
Android Application
   Development
 Sensor Programming




                             Ahsanul Karim
                ahsanul.karim@sentinelbd.com
                     Sentinel Solutions Ltd.
                    http://www.sentinelbd.com
Sensor
Programming
Android
Sensors phones support different sensors.
1. Android
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 device
5.   We’ll create a project named SensorTest for this.
     We’ll test following sensor
     list:
     1.   Accelerometer
     2.   Light
     3.   Magnetic Field
     4.   Orientation
     5.   Orientation Raw
     6.   Proximity
     7.   Temperature
     8.   Tricorder
          This is for SDK
Sensor
Programming
Android Sensors
Test
 We create a project named
 SensorTest
Sensor
Programming
Android Sensors
Test change our Activity to a
 1. We
 ListActivity
Sensor
Programming
Android Sensors
Test use SensorManager class
 2. We’ll
 3. 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
Programming
Android Sensors
Test get back to our
 6. Let’s
 ListActivity




  7. We’ll now populate the list with all sensors and check whether they are
  enabled
Sensor
Programming
Android 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
Programming
Android Sensors
TestNow we check each sensor one by
 11.
 one:
 First Magnetic field




 12. Now we check each sensor one by
 one:
 then Orientation
Sensor
Programming
Android Sensors
TestNow we check each sensor one by
 13.
 one:
 First Orientation Raw




 14. Now we check each sensor one by
 one:
 then Proximity
Sensor
Programming
Android Sensors
TestNow we check each sensor one by
 15.
 one:
 First Temperature




 16. Now we check each sensor one by
 one:
 then Tricorder
Sensor
Programming
Android Sensors
TestTo show the list we do the following by setting
 17.
  adapter



Now we run the app in emulator.

Except Accelerometer we see all sensors are
disabled
May be in later SDKs we’ll get more enabled
sensors
Sensor
Programming
Android Sensors
Test run this in Galaxy
 Lets
  tab
                            We see the following are
                            enabled:

                           1.   Accelerometer
                           2.   Light
                           3.   Magnetic Field
                           4.   Orientation
                           5.   Orientation Raw
                           6.   Proximity
                          So the sensor support varies from
                          Vendor to vendor and model to
                          model
Sensor
Programming
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.zip
   http://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
Programming
Android Sensor
Programming as an
 Let’s create a project   We create a layout with a simple
 example                  image
                          which will move according to the
                          Accelerometer:
Sensor
Programming
Android Sensor
Programming
 Our
 Activity:
Sensor
Programming
Android Sensor
Programming
 Adding SensorEventListener inner class with data
 update
Sensor
Programming
Android Sensor
Programming and registering
 Declaring variables
 listener
Sensor
Programming
Android Sensor
Programming
 Register and unregister listener on activity onStop and
 onResume:

More Related Content

What's hot

Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI WidgetsAhsanul Karim
 
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewLecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewAhsanul Karim
 
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedLecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedAhsanul Karim
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
Day1 before getting_started
Day1 before getting_startedDay1 before getting_started
Day1 before getting_startedAhsanul Karim
 
লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:Ahsanul Karim
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Ahsanul Karim
 
Lecture 3 getting active through activities
Lecture 3 getting active through activities Lecture 3 getting active through activities
Lecture 3 getting active through activities Ahsanul Karim
 
Android application-component
Android application-componentAndroid application-component
Android application-componentLy Haza
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from ScratchTaufan Erfiyanto
 
Marakana Android User Interface
Marakana Android User InterfaceMarakana Android User Interface
Marakana Android User InterfaceMarko Gargenta
 
Android Development Training
Android Development TrainingAndroid Development Training
Android Development Trainingchandutata
 

What's hot (20)

Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
 
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewLecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick Overview
 
Training android
Training androidTraining android
Training android
 
AndroidManifest
AndroidManifestAndroidManifest
AndroidManifest
 
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedLecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting Started
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Day1 before getting_started
Day1 before getting_startedDay1 before getting_started
Day1 before getting_started
 
লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:
 
Android Services
Android ServicesAndroid Services
Android Services
 
cpuk10745
cpuk10745cpuk10745
cpuk10745
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2
 
Lecture 3 getting active through activities
Lecture 3 getting active through activities Lecture 3 getting active through activities
Lecture 3 getting active through activities
 
Android application-component
Android application-componentAndroid application-component
Android application-component
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from Scratch
 
Marakana Android User Interface
Marakana Android User InterfaceMarakana Android User Interface
Marakana Android User Interface
 
Android session 2
Android session 2Android session 2
Android session 2
 
Android session 1
Android session 1Android session 1
Android session 1
 
Android Development Training
Android Development TrainingAndroid Development Training
Android Development Training
 
Android
AndroidAndroid
Android
 
Android Basic- CMC
Android Basic- CMCAndroid Basic- CMC
Android Basic- CMC
 

Viewers also liked

Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Ahsanul Karim
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAhsanul Karim
 
Action Bar Sherlock tutorial
Action Bar Sherlock tutorialAction Bar Sherlock tutorial
Action Bar Sherlock tutorialAhsanul Karim
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIAhsanul Karim
 
Android GPS Tutorial
Android GPS TutorialAndroid GPS Tutorial
Android GPS TutorialAhsanul Karim
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Ahsanul Karim
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsAhsanul Karim
 
Android MapView and MapActivity
Android MapView and MapActivityAndroid MapView and MapActivity
Android MapView and MapActivityAhsanul Karim
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesAhsanul Karim
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAhsanul Karim
 
Day 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentDay 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentAhsanul Karim
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studioParinita03
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 

Viewers also liked (16)

Client-Server
Client-ServerClient-Server
Client-Server
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
Action Bar Sherlock tutorial
Action Bar Sherlock tutorialAction Bar Sherlock tutorial
Action Bar Sherlock tutorial
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location API
 
Android GPS Tutorial
Android GPS TutorialAndroid GPS Tutorial
Android GPS Tutorial
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Mcq peresentation
Mcq  peresentationMcq  peresentation
Mcq peresentation
 
Android MapView and MapActivity
Android MapView and MapActivityAndroid MapView and MapActivity
Android MapView and MapActivity
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Day 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentDay 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver Component
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
GCM for Android
GCM for AndroidGCM for Android
GCM for Android
 

Similar to Sensors in Android (old)

Sensors on android
Sensors on androidSensors on android
Sensors on androidChinmay V S
 
Synapseindia android application development tutorial
Synapseindia android application development tutorialSynapseindia android application development tutorial
Synapseindia android application development tutorialSynapseindiappsdevelopment
 
Synapseindia android apps development tutorial
Synapseindia android apps  development tutorialSynapseindia android apps  development tutorial
Synapseindia android apps development tutorialSynapseindiappsdevelopment
 
Android app developers in bangalore- thorsignia
Android app developers in bangalore- thorsigniaAndroid app developers in bangalore- thorsignia
Android app developers in bangalore- thorsigniacharan Teja
 
Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai
Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai
Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai Itvedant
 
Android location and sensors API
Android location and sensors APIAndroid location and sensors API
Android location and sensors APIeleksdev
 
Easy steps to develop android application (tutorial)
Easy steps to develop android application (tutorial)Easy steps to develop android application (tutorial)
Easy steps to develop android application (tutorial)Dewan Razib
 
Android AppDevelopment
Android AppDevelopmentAndroid AppDevelopment
Android AppDevelopmentAshraf Ali
 
Android Training in Chandigarh | Industrial Training in Android Apps Development
Android Training in Chandigarh | Industrial Training in Android Apps DevelopmentAndroid Training in Chandigarh | Industrial Training in Android Apps Development
Android Training in Chandigarh | Industrial Training in Android Apps DevelopmentBig Boxx Animation Academy
 
Wireless sensor networks using android virtual devices and near field
Wireless sensor networks using android virtual devices and near fieldWireless sensor networks using android virtual devices and near field
Wireless sensor networks using android virtual devices and near fieldNicolas Kockel
 
Android Development in a Nutshell
Android Development in a NutshellAndroid Development in a Nutshell
Android Development in a NutshellAleix Solé
 

Similar to Sensors in Android (old) (20)

Android Sensor
Android SensorAndroid Sensor
Android Sensor
 
Sensors on android
Sensors on androidSensors on android
Sensors on android
 
Week12.pdf
Week12.pdfWeek12.pdf
Week12.pdf
 
Synapseindia android application development tutorial
Synapseindia android application development tutorialSynapseindia android application development tutorial
Synapseindia android application development tutorial
 
Synapseindia android apps development tutorial
Synapseindia android apps  development tutorialSynapseindia android apps  development tutorial
Synapseindia android apps development tutorial
 
Android tutorial1
Android tutorial1Android tutorial1
Android tutorial1
 
Android app developers in bangalore- thorsignia
Android app developers in bangalore- thorsigniaAndroid app developers in bangalore- thorsignia
Android app developers in bangalore- thorsignia
 
Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai
Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai
Everything About Android - Itvedant, Thane | Mumbai | Navi Mumbai
 
Android Sensors
Android SensorsAndroid Sensors
Android Sensors
 
Game controlling via android
Game controlling via androidGame controlling via android
Game controlling via android
 
Android location and sensors API
Android location and sensors APIAndroid location and sensors API
Android location and sensors API
 
Aptech Apps
Aptech Apps Aptech Apps
Aptech Apps
 
Control-Project
Control-ProjectControl-Project
Control-Project
 
Easy steps to develop android application (tutorial)
Easy steps to develop android application (tutorial)Easy steps to develop android application (tutorial)
Easy steps to develop android application (tutorial)
 
Android AppDevelopment
Android AppDevelopmentAndroid AppDevelopment
Android AppDevelopment
 
QR Code Scanner
QR Code Scanner QR Code Scanner
QR Code Scanner
 
Android sensor
Android sensorAndroid sensor
Android sensor
 
Android Training in Chandigarh | Industrial Training in Android Apps Development
Android Training in Chandigarh | Industrial Training in Android Apps DevelopmentAndroid Training in Chandigarh | Industrial Training in Android Apps Development
Android Training in Chandigarh | Industrial Training in Android Apps Development
 
Wireless sensor networks using android virtual devices and near field
Wireless sensor networks using android virtual devices and near fieldWireless sensor networks using android virtual devices and near field
Wireless sensor networks using android virtual devices and near field
 
Android Development in a Nutshell
Android Development in a NutshellAndroid Development in a Nutshell
Android Development in a Nutshell
 

More from Ahsanul Karim

Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesAhsanul Karim
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIAhsanul Karim
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsAhsanul Karim
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
Mobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyMobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyAhsanul Karim
 
Ui layout (incomplete)
Ui layout (incomplete)Ui layout (incomplete)
Ui layout (incomplete)Ahsanul Karim
 
Android before getting started
Android before getting startedAndroid before getting started
Android before getting startedAhsanul Karim
 

More from Ahsanul Karim (8)

Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & Preferences
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
Mobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyMobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete Study
 
List Views
List ViewsList Views
List Views
 
Ui layout (incomplete)
Ui layout (incomplete)Ui layout (incomplete)
Ui layout (incomplete)
 
Android before getting started
Android before getting startedAndroid before getting started
Android before getting started
 

Sensors in Android (old)