16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
Advertisements

Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
Programming with Android: Notifications, Threads, Services
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Lec 06 AsyncTask Local Services IntentService Broadcast Receivers.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
@2011 Mihail L. Sichitiu1 Android Introduction Application Fundamentals.
Emerging Platform#4: Android Bina Ramamurthy.  Android is an Operating system.  Android is an emerging platform for mobile devices.  Initially developed.
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
CS5103 Software Engineering Lecture 08 Android Development II.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
@2011 Mihail L. Sichitiu1 Android Introduction Platform Overview.
Operating system for mobile devices with a Java programming interface. Provides tools, e.g. a compiler, debugger, device emulator, and its own Java Virtual.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Mobile Application Development using Android Lecture 2.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
10/10/2015 E.R.Edwards 10/10/2015 Staffordshire University School of Computing Introduction to Android Overview of Android System Android Components Component.
Overview of Android Application Development
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
ANDROID L. Grewe Components  Java Standard Development Kit (JDK) (download) (latest version)  AndroidStudio.
Android 13: Services and Content Providers Kirk Scott 1.
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.
Android System Security Xinming Ou. Android System Basics An open-source operating system for mobile devices (AOSP, led by Google) – Consists of a base.
DKU-MUST Mobile ICT Education Center 10. Activity and Intent.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
Lecture 2: Android Concepts
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Android App Development. Android Architecture Linux kernel Libraries Android Runtime Application Framework Applications Application Components Activities.
Android intro Building UI #1: interactions. AndroidManifest.xml permissions 2.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Introduction to Android Programming
Lecture 2: Android Concepts
Android Application Development 1 6 May 2018
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
Activities and Intents
Android Mobile Application Development
The Android Activity Lifecycle
Reactive Android Development
Android Application Development android.cs.uchicago.edu
Android Programming Lecture 9
Developing Android Services
Application Fundamentals
Application Development A Tutorial Driven Course
Android Developer Fundamentals V2 Lesson 5
Service Services.
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
Mobile Programming Dr. Mohsin Ali Memon.
Lecture 2: Android Concepts
Mobile Programming Dr. Mohsin Ali Memon.
CIS 470 Mobile App Development
Presentation transcript:

16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin

Services One of the four primary application components: – activities – content providers – services – broadcast receivers 2

Services Application component that performs long-running operations in background with no UI application starts service and service continues to run even if original application ended or user moves to another application 3

Forms of Services Stated: – application component, such as an Activity, starts the service with the method call startService() – once started service can run in background indefinitely – generally services do not return a result (see bound service) – service should stop itself when done 4

Forms of Services Bound – application component binds itself to existing service via the bindService() method – bound service provides client-server interface that allows application component to interact with service – interact with service, send requests, get result via IPC (inter process communication – service runs as long as one or more applications bound to it – destroyed when no applications bound 5

Forms of Services Service can be started and later bound to other applications private service (manifest) cannot be bound by other applications 6

Service or Thread Past examples, kept UI thread responsive with other threads of execution, especially AsyncTask Should services be used for this? Service for actions that need to take place even if user not interacting with UI or has closed application Example, do complex rendering of image to display to user. – Not a job for a service 7

Creating a Service create subclass of Android Service class or one of its existing subclasses override callback methods that handle important aspects of service lifecycle most important of these are: – onStartCommand – startService – onBind – onCreate – onDestroy – stopSelf – stopService 8

Service Lifecycle If component starts service with startService method (leads to call to onStartCommand) service runs until it calls stopSelf or another activity calls stopService if component calls bindService (onStartCommand no called) service runs as long as at least one component bound to it 9

Service Lifecycle 10

Service Example From Roger Wallace – wanted an app that would respond to texts (SMS) received when driving and respond with a message ("Driving - Get back to you soon.") – Initial version simply auto responds to all texts – how to change it so it responds only when driving? 11

Example Service Application From The Android Developer's Cookbook SMSResponder Application Response stored in shared preferences App simply allows changes to message 12

Using SMS Permission in manifest file to send and / or receive SMS messages 13

ResponseSMS Basic App All work done in onCreate method 14

ResponseSMS onCreate 15

Service Running 16 app still running, and service has started

Simulating Texts Calls and texts can be simulated between emulators Start two emulators Use messaging app to send text Phone number is simply the emulator port number (visible at top of the emulator or in eclipse) 17

Dual Emulators 18

Emulator Texts 19

Testing Service 20

Creating a Service Extend the Service class – adapter class exists, IntentService that handles a lot of the details override onStartCommand – return an int describing what system should do for starting service – START_NOT_STICKY, if system kills service don't restart – START_STICKY, if system kills service then recreate, but does not redeliver intent – START_REDELIVER_INTENT, if system kills service then recreate and redeliver last intent 21

SMS Responder 22

SMS Responder - onCreate 23

Broadcast Receivers The fourth main application component "A broadcast receiver is a component that responds to system-wide broadcast announcements." Android system sends multiple kinds of broadcasts – screen turned off, battery low, picture captured, SMS received, SMS sent 24

Broadcast Receivers Applications can initiate broadcasts to inform other applications of status or readiness Don't display UI – may create status bar notifications Usually just a gateway to other components and does very minimal work – initiate service to perform based on some event Broadcasts are delivered as Intents 25

Broadcast Receivers receive intents sent by sendBroadcast() method LocalBroadcastManager to send Broadcasts within your application only In SMS responder register receivers unregister when service destroyed key point: override the onReceive method for BroadcastReceiver subclass 26

BroadcastReceivers What broadcasts are available? Check the Intent class roid/content/Intent.html roid/content/Intent.html – search for "Broadcast Action" Also look in android-sdk\platforms\ \data\ broadcast_actions.txt 27

Broadcasts 28

Broadcasts from broadcast_ actions.txt in sdk files platforms-> -> data\ 29

SMS Received - Broadcast Receiver 30

SMS Data The SMS data in the Bundle (map) is under the key "pdus" – pdu, protocol data unit (some sources indicate protocol description unit) 31

respond method incoming SMS messages trigger respond method 32

Stopping Service Once started service runs until device shut down Starts again when app started again Add option to start and shut down the service 33

Starting Service 34

Checking Running Processes 35