Xin Pan. CSCI Fall

Similar documents
ELET4133: Embedded Systems. Topic 15 Sensors

Spring Lecture 9 Lecturer: Omid Jafarinezhad

CE881: Mobile & Social Application Programming

Sensor & SensorManager SensorEvent & SensorEventListener Filtering sensor values Example applications

filled by the user and display it by setting the value of view elements. Uses display.xml as layout file to display the result.

App Development for Smart Devices. Lec #8: Android Sensors

Threads. Marco Ronchetti Università degli Studi di Trento

Topics Related. SensorManager & Sensor SensorEvent & SensorEventListener Filtering Sensor Values Example applications

Android Application Development

Introduction To Android

UNDERSTANDING ACTIVITIES

Real-Time Embedded Systems

Embedded Systems Programming - PA8001

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Android Apps Development for Mobile Game Lesson 5

Mobile Programming Lecture 9. Bound Services, Location, Sensors, IntentFilter

Vienos veiklos būsena. Theory

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Sensors. Marco Ronchetti Università degli Studi di Trento

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android App Development. Muhammad Sharjeel COMSATS Institute of Information Technology, Lahore

Android Fundamentals - Part 1

Johnson Sun. Application / SASD

Adapters. Marco Ronchetti Università degli Studi di Trento

API Guide for Gesture Recognition Engine. Version 2.0

Android Ecosystem and. Revised v4presenter. What s New

Introduction to Android

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

Mobile Computing. Introduction to Android

Exercise 1: First Android App

Developer s overview of the Android platform

Working with Sensors & Internet of Things

Introduction to Android

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

API Guide for Gesture Recognition Engine. Version 1.1

LECTURE NOTES OF APPLICATION ACTIVITIES

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Android. Operating System and Architecture. Android. Screens. Main features

Introduction to Android

Computer Science E-76 Building Mobile Applications

Android Overview. Most of the material in this sec5on comes from h6p://developer.android.com/guide/

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Comp 595MC/L: Mobile Computing CSUN Computer Science Department Fall 2013 Midterm

Programming with Android: System Services. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android Development Tutorial. Yi Huang

COMP4521 EMBEDDED SYSTEMS SOFTWARE

App Development for Android. Prabhaker Matet

Android HelloWorld - Example. Tushar B. Kute,

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

CS 4330/5390: Mobile Application Development Exam 1

Android Overview. Most of the material in this section comes from

Android App Development

Lab 5 Periodic Task Scheduling

Programming with Android: System Architecture. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

COLLEGE OF ENGINEERING, NASHIK-4

Lifecycle Callbacks and Intents

EMBEDDED SYSTEMS PROGRAMMING Android Services

Android Services. Victor Matos Cleveland State University. Services

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

BCA 6. Question Bank

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

ANDROID SYLLABUS. Advanced Android

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Android Software Development Kit (Part I)

API Guide for Gesture Recognition Engine. Version 1.3

Android.Test-inside.OA0-002.v by.Andres.70q. Exam Code: OA Exam Name: Android Application Engineer Certifications Basic

Android Overview. Francesco Mercaldo, PhD

Lecture 1 Introduction to Android. App Development for Mobile Devices. App Development for Mobile Devices. Announcement.

Android Activities. Akhilesh Tyagi

CSCU9YH Development with Android

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

Mobile App Design Project Doodle App. Description:

Assignment 1. Start: 28 September 2015 End: 9 October Task Points Total 10

Agenda. The Android GUI Framework Introduction Anatomy A real word example Life cycle Findings

Introduction to Android development

Overview of Activities

UNIT:2 Introduction to Android

Activities. Repo:

Vendor: Android. Exam Code: OA Exam Name: Android Application Engineer Certifications Basic. Version: Demo

Mobila applikationer och trådlösa nät, HI1033, HT2012

MAD Gaze x HKCS. Best Smart Glass App Competition Developer Guidelines VERSION 1.0.0

Mobile Application (Design and) Development

UI Fragment.

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Android - open source mobile platform

CS378 -Mobile Computing. Services and Broadcast Receivers

An Overview of the Android Programming

<uses-permission android:name="android.permission.internet"/>

Android System Development Day - 3. By Team Emertxe

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Design and Implementation of Somatosensory Teaching Pendant System Based on Android Platform

Android Training Overview (For Demo Classes Call Us )

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Transcription:

Xin Pan CSCI5448 2011 Fall

Outline Introduction of Android System Four primary application components AndroidManifest.xml Introduction of Android Sensor Framework Package Interface Classes Examples of Using Accelerometer Using background Service Using foreground Activity

Introduction Android Inc. is acquired by Google in 2005. Open Handset Alliance was established and Android was announced in 2007. The first Android handset and source code was released in 2008. Open and comprehensive platform for mobile devices. Platform is powered by Linux.

Android Version History Version Release Date 1.x Sep 2008 Sep 2009 2.x Oct 2009 Dec 2010 Linux Kernel 2.6.23/ 27/29 2.6.29/ 32/35 Selected Key Updates Camera, WiFi, and Bluetooth supported. Bluetooth 2.1, API changes, system speed, memory, and performance optimizations, media support, video chat. 3.x (Honeyc omb) Feb 2011 Jul 2011 2.6.36 The first SDK release for tablet computers. Motorola Xoom tablet is the first device featuring this version. 4.x (Ice Cream Sandwic h) Oct 2011 3.0.1 Face Unlock, Wi-Fi Direct. Galaxy Nexus is the first device featuring this version. added facial recognition, social networking, information sharing, and other features.

Android Architecture This diagram shows the major components of Android operating system. http://developer.android.com/guide/basics/what-is-android.html

Android Architecture Android software layers consists of: Linux Provides process and memory management, security, networking, and device drivers. Libraries Runtime Dalvik VM Application Framework provides services to applications, such as notification and activity managers. These are all implemented as Java classes. Applications Component-oriented and integration-oriented

Android Application Written in Java programming language. Packaged into a.apk file. Runs isolated in its own VM. Composes of one or more application components. Starts the components when needed. Ends the components when no longer needed.

Application Components Android process has four primary components: Activities a component that provides a user interface, e.g. send an email. Services a component that can perform long-running background operations without user interface. Content providers a component that manages application data Broadcast receivers a component that responds to system-wide broadcast announcements.

Activities Android is sensitive to the lifecycle of an application and its components. Android provides callbacks to process state changes. Lifecycle callbacks for an activity oncreate() OnStart() OnRestart() OnResume() OnPause() OnStop() ondestory() http://developer.android.com/guide/topics/fundamentals/activities.html

Services A service runs in the background. A service needs to be declared in the mainifest <manifest... >... <application... > <service android:name=".exampleservice" />... </application> </manifest> Services can be started with Context.startService() and Context.bindService(). Service will only stop when Context.stopService() or stopself() is called. Context.bindService() can be used to obtain a persistent connection to a service.

Service Lifecycle Service lifecycle callback methods are used to monitor changes in a service s state. oncreate() onstartcommand() Or onbind() and onunbind() ondestory() http://developer.android.com/guide/topics/fundamentals/services.html

Content Providers Content providers store and retrieve data. android.provider package The information needed to query a content provider, URI to identify the provider A Uniform Resource Identifier that identifies an abstract or physical resource The name of the data fields The data types of the fields Audio, video, images

Broadcast Receivers BroadcastReceiver object is only valid during the call to onreceive(). Once onreceive() returns, BroadcastReceiver is no longer active, and system will consider its process to be empty and kill the process. Therefore, for long-running operations, Service and BroadcastReceiver should be used together to keep the process active.

The Manifest File Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory. AndroidManifest.xml defines all the components, contents and behavior of the application, e.g. activities and services. <application> <activity/> <service/> <receiver/> <provider/> </application> xml class will parse the contents

Sensor Types Android supports multiple types of sensors Light sensor Proximity sensor Temperature sensor Pressure sensor Gyroscope sensor Accelerometer Magnetic field sensor Orientation sensor Gravity sensor Linear acceleration sensor Rotation vector sensor Near Field Communication (NFC) sensor GPS (GPS is similar to a sensor, but not a sensor)

Android Sensor Framework Layers from bottom to top Sensor driver Sensor Hardware Module Sensor JNI Java Sensor Class Java Application http://processors.wiki.ti.com/index.php/android_sensor_portingguide

Sensor Package and Classes Package: android.hardware Interface SensorEventListener Classes: Sensor SensorEvent SensorManager

Interface: SensorEventListener (I) Used for receiving notifications from the SensorManager when sensor values have changed. Public methods: abstract void onsensorchanged(sensorevent event) abstract void onaccuracychanged(sensor sensor, int accuracy)

Interface: SensorEventListener (II) abstract void onsensorchanged(sensorevent event) This function is called by system when sensor values have changed. This is an abstract function, need to be implemented by user. The parameter of this function is an instance of Class SensorEvent (will introduce this class later), which holds information such as sensor type and sensor values.

Interface: SensorEventListener (III) abstract void onaccuracychanged(sensor sensor, int accuracy) This function is called when the accuracy of a sensor has changed. This is an abstract function, need to be implemented by user. The parameters of this function are an instance of Class Sensor (will introduce this class later) and the new accuracy level ( High(=3), Medium(=2), and Low(=1) ).

Class: Sensor (I) Represents a sensor Use getsensorlist(int) to get the list of available Sensors in Class SensorManager.

Class: Sensor (II) Class Sensor contains several constants to represent Android sensor type Constant TYPE_ACCELEROMETER TYPE_ALL TYPE_AMBIENT_TEMPERATURE TYPE_GRAVITY TYPE_GYROSCOPE TYPE_LIGHT TYPE_LINEAR_ACCELERATION TYPE_MAGNETIC_FIELD TYPE_PRESSURE TYPE_PROXIMITY TYPE_RELATIVE_HUMIDITY TYPE_ROTATION_VECTOR Sensor an accelerometer sensor type all sensor types an ambient temperature sensor type a gravity sensor type a gyroscope sensor type an light sensor type a linear acceleration sensor type a magnetic field sensor type a pressure sensor type an proximity sensor type a relative humidity sensor type a rotation vector sensor type

Class: Sensor (III) This class also includes a set of functions to get the properties of a sensor, such as maximum range of the sensor in the sensor's unit. name string of the sensor. the power in ma used by this sensor while in use resolution of the sensor in the sensor's unit. generic type of this sensor. vendor string of this sensor. version of the sensor's module.

Class: SensorEvent (I) Represents a sensor event and holds information. Sensor event information includes: The accuracy of the sensor data The sensor that generated this event. The time in nanosecond at which the event happened Sensor data array. The length and contents of the values array depends on which sensor type is being monitored.

Class: SensorEvent (II) Sensor data Examples Sensor type is Sensor.TYPE_ACCELEROMETER Accelerometer has three directions: vertically, laterally, or longitudinally (X, Y, Z) All values are in SI units (m/s^2) values[0]: Acceleration minus Gx on the x-axis values[1]: Acceleration minus Gy on the y-axis values[2]: Acceleration minus Gz on the z-axis Sensor type is Sensor.TYPE_GYROSCOPE All values are in radians/second and measure the rate of rotation around the device's local X, Y and Z axis. values[0]: Angular speed around the x-axis values[1]: Angular speed around the y-axis values[2]: Angular speed around the z-axis

Class: SensorManager (I) SensorManager provides sensor management services to other applications on the device. provides a sensor selector package provides a standard way to all supported sensors Provides an interface to list and invoke the sensors Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.

Class: SensorManager (II) An important Function registerlistener (SensorEventListener listener, Sensor sensor, int rate) Registers a SensorEventListener for the given sensor. You can make a single SensorManager, but for each sensor you want to track, you need to make a unique SensorEventListener, and Sensor. To avoid the unnecessary usage of battery, you should register the listener in the onresume method and unregister in the onpause method when overriding Activity methods Listener- A SensorEventListener object. Sensor - The Sensor to register to. Rate - The rate sensor events are delivered at.

Class: SensorManager (III) Delivering rate for sensor events must be one of : Constants SENSOR_DELAY_FASTEST SENSOR_DELAY_GAME SENSOR_DELAY_NORMAL SENSOR_DELAY_UI get sensor data as fast as possible rate suitable for games rate (default) suitable for screen orientation changes rate suitable for the user interface

Read Accelerometer It can be read in background service or foreground activity. We will look at both examples: Service write accelerometer data into log file Activity display accelerometer data on screen

Write Accelerometer into Log Steps: Create an accelerometer Service and implement a SensorEventListener Implement onaccuracychanged and onsensorchanged method Create variables for SensorManager and Sensor Get Object of SensorManager using system service Get Object of Acc Sensor from SensorManager Register a SensorEventListener for the accelerometer sensor

Step1 Create an accelerometer Service and implement a SensorEventListener interface to process sensor data and sensor accuracy change Class AccServcie extend Service implements SensorEventListener { public void onsensorchanged(sensorevent event) { // deal with sensor data mnewvalue = (int) event.values[0]*10; Log.d(TAG, Integer.toString(mNewValue)); } } public void onaccuracychanged(sensor sensor, int accuracy) { // deal with sensor accuracy change }

Step2 Create and get instants of SensorManager and Sensor Class AccService extends Service implements SensorEventListener { @Override public void oncreate() { SensorManager sensormanager = (SensorManager) getsystemservice(sensor_service); } } Sensor accsensor = sensormanager.getdefaultsensor( Sensor.TYPE_ACCELEROMETER);

Step3 Register a SensorEventListener for the accelerometer sensor. Class AccService extends Service implements SensorEventListener { @Override public void oncreate() { SensorManager sensormanager = (SensorManager) getsystemservice(sensor_service); Sensor accsensor = sensormanager.getdefaultsensor( Sensor.TYPE_ACCELEROMETER); } } sensormanager.registerlistener( this, accsensor, SensorManager.SENSOR_DELAY_NORMAL);

Read Accelerometer in Foreground Steps: Add a main.xml in /res/layout folder main.xml describe the layout of the screen display Similar to that of Service, Create an accelerometer Activity and implement a SensorEventListener interface to process sensor data and sensor accuracy change Create and get instants of SensorManager and Sensor, and register a SensorEventListener Implement activity life cycle management for sensor reading

Step1 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="fill_parent android:layout_height="fill_parent"> <TextView android:id="@+id/textview android:layout_width="match_parent android:layout_height="match_parent android:text="shake to get a toast and to switch color" /> </LinearLayout>

Step2 Create an accelerometer Activity and implement a SensorEventListener interface to process sensor data and sensor accuracy change Class AccActivity extends Activity implements SensorEventListener { public void onsensorchanged(sensorevent event) { // deal with sensor data TextView tvx= (TextView)findViewById(R.id.x_axis); mnewvalue = (int) event.values[0]*10; tvx.settext(float.tostring(mnewvalue )); } } public void onaccuracychanged(sensor sensor, int accuracy) { // deal with sensor accuracy change }

Step3 Create and get instants of SensorManager and Sensor, and register a SensorEventListener for the accelerometer sensor Class AccActivity extends Activity implements SensorEventListener { @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); SensorManager sensormanager = (SensorManager) getsystemservice(context.sensor_service); Sensor accsensor = sensormanager.getdefaultsensor(sensor.type_accelerometer); } } // Register for events. sensormanager.registerlistener( this, accsensor, SensorManager.SENSOR_DELAY_NORMAL);

Step4 For activity life cycle management, onresume and onpause need to be overridden. Register a listener when receiving data from accelerometer Turn off the listener when not listening Class AccActivity extends Activity implements SensorEventListener { protected void onresume() { super.onresume(); } msensormanager.registerlistener(this, maccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onpause() { super.onpause(); } msensormanager.unregisterlistener(this); unregister the listener to save energy

Reference http://developer.android.com Komatineni, S., MacLean, D., and Hashimi, S. (2011). Pro Android 3. Apress, 2011. Retrieved from http://books.google.com/books