1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

Programming with Android: Activities
Programming with Android: Activities
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Sensors.  Hardware devices that take measurements of the physical environment  Some examples  3-axis Accelerometer  3-axis Magnetic field sensor 
Location & Maps.  Mobile applications can benefit from being location-aware, e.g.,  Routing from a current to a desired location  Searching for stores.
Android wifi-based localization. Localization types Android allows (location providers) – GPS (GPS_PROVIDER) – Cell tower + wifi (NETWORK_PROVIDER) You.
Android sensors.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Mobile Application Development Selected Topics – CPIT 490
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Lecture # 9 Hardware Sensor. Topics 2  SensorManger & Sensor  SensorEvent & SensorEventListener  Example Application.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
1 Localization and Sensing Nilanjan Banerjee Mobile Systems Programming (Acknowledgement: Jules White) University of Arkansas Fayetteville, AR
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Sensing. Possible sensors on devices – Documented in SensorEvent class Accelerometer (m/s 2 ) – acceleration in x, y, z axes Magnetic Field (micro Tesla)
Doodlz App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
Android Accessing GPS Ken Nguyen Clayton State University 2012.
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Sensors – Part I SE 395/595.
Android – Fragments L. Grewe.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Noname. Conceptual Parts States of Activities Active Pause Stop Inactive.
Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within.
Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.
Android Application Lifecycle and Menus
CS378 - Mobile Computing Audio.
Sensors – Part 2 SE 395/595. Location in Android LocationManager class – Configure providers and their listeners LocationListener class – Handles update.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.
CPE 490/590 – Smartphone Development
The Doodlz app enables you to paint by dragging one or more fingers across the screen. The app provides options for setting the drawing color.
1. 2 Android location services Determining a device’s current location Tracking device movements Proximity alerts.
CS499 – Mobile Application Development Fall 2013 Location & Maps.
Android Android Sensors Android Sensors: – Accelerometer – Gravity sensor – Linear Acceleration sensor – Magnetic Field sensor – Orientation.
Sensors in Android.
Vijay Kumar Kolagani Dr. Yingcai Xiao
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
Concurrency in Android
Activity and Fragment.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
What's Happening Today Working with Images
Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the.
Anatomy of an Android App and the App Lifecycle
Android Location Based Services
The Android Activity Lifecycle
Sensors, maps and fragments:
Vijay Kumar Kolagani Dr. Yingcai Xiao
Location Service and Sensors
CS499 – Mobile Application Development
Activities and Intents
Android Programming Tutorial
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Mobile Programming Sensors in Android.
CS378 - Mobile Computing Location and Maps.
SE4S701 Mobile Application Development
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County

2 Today’s lecture Android Lifecycle Activity starts up Activity suspends and resumes Activity is stopped and restarted.

3 Starting an activity Activity starts onCreate(), onStart(), onResume() are called in succession onStart() Activity is partially visible Ack: Android Development website

4 Pausing and Resuming an Activity How is pausing defined? If an activity is partially visible in the background Dialog boxes open up Method called is onPause() What should you do before an activity pauses save state of the application? release resources

5 Stopping and Starting an Activity How is stopping an activity defined? If the activity is not visible Press back or home button Start a new activity or receive a phone call System destroys the activity What should you do before an activity stops save state of the application? release resources

6 Recreating an Activity If the system destroys an activity due to system constraints Saves certain state of your activity that is restored back the next time the activity starts If you want to save your own state before the activity is destroyed: Override the onSaveInstanceState(Bundle) Access your state during onCreate() or onRestoreInstanceState()

Android Sensors Overview Android Sensors: MIC Camera Temperature Location (GPS or Network) Orientation Accelerometer Proximity Pressure Light

Two types of sensors on the android platform Hardware sensors –Physical sensors present on the phone –Accelerometers, temperature, gyroscope Software sensors –Virtual sensors that are built on top of hardware sensors. –Orientation sensors --- accelerometer + gyroscope?

Async Callbacks Android’s sensors are controlled by external services and only send events when they choose to An app must register a callback to be notified of a sensor event Each sensor has a related XXXListener interface that your callback must implement –E.g. LocationListener SensorManager Your App Sensor Event Register Callback

Getting the Relevant System Service The non-media (e.g. not camera) sensors are managed by a variety of XXXXManager classes: –LocationManager (GPS) –SensorManager (accelerometer, gyro, proximity, light, temp) The first step in registering is to obtain a reference to the relevant manager Every Activity has a getSystemService() method that can be used to obtain a reference to the needed manager public class MyActivity … { private SensorManager sensorManager_; public void onCreate(){ … sensorManager_ = (SensorManager) getSystemService(SENSOR_SERVICE); }

Registering for Sensor Updates –The SensorManager handles registrations for Accelerometer, Temp, Light, Gyro –In order for an object to receive updates from a sensor, it must implement the SensorEventListener interface –Once the SensorManager is obtained, you must obtain a reference to the specific sensor you are interested in updates from –The arguments passed into the registerListener method determine the sensor that you are connected to and the rate at which it will send you updates public class MyActivity … implements SensorListener{ private Sensor accelerometer_; private SensorManager sensorManager_; public void connectToAccelerometer() { sensorManager_ = (SensorManager)getSystemService(SENSOR_MANAGER); accelerometer_ = sensorManager_.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager_.registerListener(this, accelerometer_, SensorManager.SENSOR_DELAY_NORMAL); }

The SensorEventListener Interface public class MyActivity … implements SensorListener{ // Called when a registered sensor changes public void onSensorChanged(SensorEvent sensorEvent) { if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { float xaccel = sensorEvent.values[0]; float yaccel = sensorEvent.values[1]; float zaccel = sensorEvent.values[2]; } // Called when a registered sensor's accuracy public void onAccuracyChanged(Sensor arg0, int arg1) { // TODO Auto-generated method stub } –Because there is one interface for multiple types of sensors, listening to multiple sensors requires switching on the type of event (or creating separate listener objects) –Also forces registration at the same rate per listener –Simple approach:

The SensorEventListener Interface public class MyActivity … { private class AccelListener implements SensorListener { public void onSensorChanged(SensorEvent sensorEvent) { … } public void onAccuracyChanged(Sensor arg0, int arg1) {} } private class LightListener implements SensorListener { public void onSensorChanged(SensorEvent sensorEvent) { … } public void onAccuracyChanged(Sensor arg0, int arg1) {} } private SensorListener accelListener_ = new AccelListener(); private SensorListener lightListener_ = new LightListener(); … public void onResume(){ … sensorManager_.registerListener(accelListener, accelerometer, SensorManager.SENSOR_DELAY_GAME); sensorManager_.registerListener(lightListener, lightsensor, SensorManager.SENSOR_DELAY_NORMAL); } public void onPause(){ sensorManager_.unregisterListener(accelListener_); sensorManager_.unregisterListener(lightListener_); } –Another approach for multiple sensors (probably better):

Android System Services –Each App runs in its own process –Each Android system service, such as the SensorManager, also runs in its own thread –This has important implications: 1.Communication with the system services is through IPC 2.The thread that delivers an event will be a special thread that is dedicated to processing incoming IPC calls 3.If you directly update the GUI from any thread other than the display thread, bad things happen

How to Update the GUI with Sensor Data –Android has a built in mechanism for queuing work that needs to be run on the display thread –The Handler class allows you to create a queue inside of your Activity that can store work for the display thread –You create a handler once and then post work to it public class MyActivity … implements SensorListener{ private class AccelWork implements Runnable { private Location data_; public AccelWork(Location d){data_ = d;} public void run(){ //do something with the data to the GUI } private Handler myHandler_ = new Handler(); // Called when a registered sensor changes public void onSensorChanged(SensorEvent sensorEvent) { AccelWork work = new AccelWork(sensorEvent); myHandler_.post(work); }

How to Update the GUI with Sensor Data –The lazy approach public class MyActivity … implements SensorListener{ private Handler myHandler_ = new Handler(); // Called when a registered sensor changes public void onSensorChanged(final SensorEvent sensorEvent) { myHandler_.post( new Runnable(){ public void run(){ //do something with the sensorEvent data //to the gui } ); }

17 What is localization, aka location? absolute location (lat, long) relative location context location

18 Why should I care about localization? social application advertisements podcasting

Registering for Location Updates –The LocationManager handles registrations for GPS and network location updates –In order for an object to receive updates from GPS, it must implement the LocationListener interface –Once the LocationManager is obtained, an object registers for updates by calling requestLocationUpdates (there are multiple versions you can use) –The arguments passed into the requestLocationUpdates method determine the granularity of location changes that will generate an event send updates that are at least X meters apart send updates at least this far apart in time send updates that have this minimum accuracy public class MyActivity … implements LocationListener{ private LocationManager locationManager_; public void onCreate(){ … locationManager_ = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager_.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, Criteria.ACCURACY_FINE, this); }

Location Providers –The phone’s location can be determined from multiple providers GPS Network –GPS location updates consume significantly more power than network location updates but are more accurate GPS: 25 seconds * 140mA = 1mAh Network: 2 seconds * 180mA = 0.1mAh –The provider argument determines which method will be used to get a location for you –You can also register for the PASSIVE_PROVIDER which only updates you if another app is actively using GPS / Network location public class MyActivity … implements LocationListener{ private LocationManager locationManager_; public void onCreate(){ … locationManager_ = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager_.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 10, Criteria.ACCURACY_FINE, this); }

The LocationListener Interface public class MyActivity … implements LocationListener{ … // Called when your GPS location public void onLocationChanged(Location location) { } // Called when a provider gets turned off by the user in the public void onProviderDisabled(String provider) { } // Called when a provider is turned on by the user in the public void onProviderEnabled(String provider) { } // Signals a state change in the GPS (e.g. you head through a tunnel and // it loses its fix on your public void onStatusChanged(String provider, int status, Bundle extras) { }

Location Information public class MyActivity … implements LocationListener{ … // Called when your GPS location public void onLocationChanged(Location location) { double altitude = location.getAltitude(); double longitude = location.getLongitude(); double latitude = location.getLatitude(); float speed = location.getSpeed(); float bearing = location.getBearing(); float accuracy = location.getAccuracy(); //in meters long time = location.getTime(); //when the fix was obtained // Other useful Location functions: // // location.distanceTo(dest) // location.bearingTo(dest) }

Being a Good Citizen… It is very important that you unregister your App when you no longer need updates For example, you should always unregister your listener when your Activity is paused! If you unregister when you pause, you must also reregister when you resume –This is true for all sensors! public class MyActivity … { private LocationManager locationManager_; public void onCreate(Bundle savedInstanceState) { … locationManager_ = (LocationManager)getSystemService(LOCATION_SERVICE); } protected void onPause() { super.onPause(); locationManager_.removeUpdates(this); } protected void onResume() { super.onResume(); locationManager_.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, Criteria.ACCURACY_FINE, this); } … }