Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.

Slides:



Advertisements
Similar presentations
Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
Advertisements

Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
CSE2102 Introduction to Software Engineering Lab 2 Sept/4/2013.
Chapter 6 Jam! Implementing Audio in Android Apps.
CSS216 MOBILE PROGRAMMING Android, Chapter 9 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Lec 06 AsyncTask Local Services IntentService Broadcast Receivers.
1 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Android Services Mobile Application Development Selected Topics – CPIT Oct-15.
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.
로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Broadcasting Service 2.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Working in the Background Radan Ganchev Astea Solutions.
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.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Services 1 CS440. What is a Service?  Component that runs on background  Context.startService(), asks the system to schedule work for the service, to.
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
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.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
Speech Service & client(Activity) 오지영.
Android intro Building UI #1: interactions. AndroidManifest.xml permissions 2.
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.
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
Android Application -Architecture.
Permissions.
Lecture 2: Android Concepts
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
Lecture 7: Android Services
Android Boot Camp for Developers Using Java, 3E
Concurrency in Android
CS240: Advanced Programming Concepts
Cleveland State University
Notifications and Services
Android Mobile Application Development
Reactive Android Development
Android Application Development android.cs.uchicago.edu
Android Programming Lecture 9
Developing Android Services
CIS 470 Mobile App Development
CS371m - Mobile Computing Responsiveness.
Android Topics Android Activity Lifecycle and Experiment Toast
Android Topics Asynchronous Callsbacks
Android Developer Fundamentals V2
Android Developer Fundamentals V2 Lesson 5
Threads in Java James Brucker.
Service Services.
CIS 470 Mobile App Development
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
Lecture 2: Android Concepts
Mobile Programming Dr. Mohsin Ali Memon.
Android Threads Dimitar Ivanov Mobile Applications with Android
CIS 470 Mobile App Development
Presentation transcript:

Developing Android Services

Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated tasks in a service Having an activity and a service to communicate 2

Android Service Application component that runs (in background) without needing to interact with the user E.g., playing background music, logging geographical coordinates => no need to present a UI to the user Q: Other components of Android apps? 3

Creating Service As a subclass of android.app.Service Override key framework methods including: int onStartCommand(Intent i, int flags, int startId) IBind onBind(Intent i) void onCreate() void onDestroy() Declare the service in AndroidManifest.xml, e.g., 4

5 int onStartCommand(Intent intent, int flags, int startId) Called when the service is started by startService() startId: unique Id generated by system flags: additional info supplied by the system Return START_STICKY to make the service continue to run until explicitly stopped ( START_NOT_STICKY). call stopSelf() or stopService() to stop the service IBind onBind(Intent intent) Enable to bind an activity (by calling bindService()) to a service so as to access public elements such as fields and methods void onCreate() Called when the service is first created void onDestroy() Called when the service is stopped by calling stopService()

Starting/Stopping Service startService(new Intent(getBaseContext(), MyService.class)); startService(new Intent("edu.utep.cs.cs4330.MyService"));  Need to make the service available to other applications *Service runs on the same thread that started it (e.g., UI thread). stopService(new Intent(getBaseContext(), MyService.class)); 6

Long-Running Tasks In Service Perform in a new thread, but why? How? Thread class AsyncTask class void onPreExecute() Invoked on the UI thread before doInBackground void doInBackground(Params...) Invoked on the background thread after onPreExecute void onProgressUpdate(Progress...) Invoked on UI thread after a call to publishProgress(Progress...) void onPostExecute(Result) Invoked on the UI thread after the background task finishes 7

Repeated Tasks In Service Performing repeated tasks in a service, e.g., alarm clock service that runs persistently in background Use the Timer class providing several methods such as void scheduleAtFixedRate(TimerTask task, long delay, long period) 8 new Timer().scheduleAtFixedRate( new TimerTask() { public void run() { /*... */ } }, 0, // no delay 1000); // at every sec To cancel, call the Timer.cancel() method.

IntentService Subclass of Service to execute an asynchronous task on a new thread Automatically stop the service when completed (i.e., no need to call the stopSelf() method) Just need to override: protected void onHandleIntent(Intent i) that performs a task on a new thread Also need to add the service to manifest, e.g., 9

Invoking Activity From Service Use the BroadCastReceiver 10 In service: Intent broadcastIntent = new Intent(); broadcastIntent.setAction("MyAction"); getBaseContext().sendBroadcast(broadcastIntent); In activity: IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("MyAction"); registerReceiver(new BroadcastReceiver() { public void onReceive(Context context, Intent intent) {... } }, intentFilter);

Binding Activities To Services For an activity to access public members of a service Use the bindService() method in Activities Override the onBind() method in Services Refer to API document of the Service class 11

In-Class (Pair): Music app 12 Create an app that, given an URI of a music file, plays the music as a background service. When the playback of the music file is completed, the service should notify to the main activity to display a toast message indicating the completion of the playback. (Hint: use the MediaPlayer class) MediaPlayer public static MediaPlayer create(Context, Uri) public void start() public void setOnCompletionListener( MediaPlayer.OnCompletionListener)