Services. service: A background task used by an app.

Similar documents
Android Service. Lecture 19

MOBILE APPLICATION DEVELOPMENT LECTURE 10 SERVICES IMRAN IHSAN ASSISTANT PROFESSOR

Mobile Programming Practice Background processing AsynTask Service Broadcast receiver Lab #5

EMBEDDED SYSTEMS PROGRAMMING Android Services

Services. Background operating component without a visual interface Running in the background indefinitely

CS378 -Mobile Computing. Services and Broadcast Receivers

Upcoming Assignments Quiz Friday? Lab 5 due today Alpha Version due Friday, February 26

Embedded Systems Programming - PA8001

Efficient Android Threading

Multiple Activities. Many apps have multiple activities

Overview of Activities

Services. Marco Ronchetti Università degli Studi di Trento

Mobile Application Development Android

CS 193A. Multiple Activities and Intents

Mobile Application Development Android

Notifications and Services. Landon Cox March 28, 2017

Software Practice 3 Today s lecture Today s Task

Programming with Android: Notifications, Threads, Services. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Android Services & Local IPC: The Command Processor Pattern (Part 1)

Outline. Admin. Example: TipCalc. Android: Event Handler Blocking, Android Inter-Thread, Process Communications. Recap: Android UI App Basic Concepts

Android. Broadcasts Services Notifications

Android Activities. Akhilesh Tyagi

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Writing Efficient Drive Apps for Android. Claudio Cherubino / Alain Vongsouvanh Google Drive Developer Relations

Answers to Exercises

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

Android Ecosystem and. Revised v4presenter. What s New

App Development for Smart Devices. Lec #7: Windows Azure

Understanding Application

CMSC436: Fall 2013 Week 3 Lab

Android Services & Local IPC: Overview of Programming Bound Services

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Services Broadcast Receivers Permissions

Android Fundamentals - Part 1

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

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

Minds-on: Android. Session 2

Services are software components designed specifically to perform long background operations.

32. And this is an example on how to retrieve the messages received through NFC.

Mobile and Ubiquitous Computing: Android Programming (part 4)

Mobile Application Development Android

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Mobile Computing. Introduction to Android

LECTURE 12 BROADCAST RECEIVERS

OFFLINE MODE OF ANDROID

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

Podstawowe komponenty aplikacji, wymiana międzyprocesowa

Analyzing Wi Fi P2P in the context of a hangman game 1

Application Fundamentals

Chris Guzman. Developer chris-guzman.com

The Broadcast Class Registration Broadcast Processing

Android Services. Victor Matos Cleveland State University. Services

ANDROID SERVICES, BROADCAST RECEIVER, APPLICATION RESOURCES AND PROCESS

Services. Marco Ronchetti Università degli Studi di Trento

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

INTRODUCTION TO ANDROID

Termite WiFi Direct API

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

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Broadcast receivers. Marco Ronchetti Università degli Studi di Trento

ANDROID TRAINING PROGRAM COURSE CONTENT

BITalino Java Application Programming Interface. Documentation Android API

CS434/534: Topics in Networked (Networking) Systems

Mobile Application Development Android

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Android Basics. Android UI Architecture. Android UI 1

Intents & Intent Filters. codeandroid.org

Services & Interprocess Communication (IPC) CS 282 Principles of Operating Systems II Systems Programming for Android

Tishik Int. University / College of Science / IT Dept. This Course based mainly on online sources ADVANCED MOBILE APPLICATIONS / Spring 1

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

CSCU9YH Development with Android

Solving an Android Threading Problem

Android Architecture and Binder. Dhinakaran Pandiyan Saketh Paranjape

Android Essentials with Java

CS 193A. Activity state and preferences

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

ANDROID DEVELOPMENT. Course Details

Using Libraries, Text-to-Speech, Camera. Lecture 12

Midterm Examination. CSCE 4623 (Fall 2017) October 20, 2017

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Multithreading and Interactive Programs

Programming with Android: Activities and Intents. Dipartimento di Informatica Scienza e Ingegneria Università di Bologna

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

CS378 -Mobile Computing. Intents

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

App Development for Android. Prabhaker Matet

Embedded Systems Programming - PA8001

ANDROID SYLLABUS. Advanced Android

CMSC436: Fall 2013 Week 4 Lab

Introduction to Android

INTRODUCTION COS MOBILE DEVELOPMENT WHAT IS ANDROID CORE OS. 6-Android Basics.key - February 21, Linux-based.

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

COSC 3P97 Mobile Computing

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Lifecycle-Aware Components Live Data ViewModel Room Library

Real-Time Embedded Systems

Android Application Development using Kotlin

Import the code in the top level sdl_android folder as a module in Android Studio:

Mobile Development Lecture 8: Intents and Animation

Transcription:

CS 193A Services This document is copyright (C) Marty Stepp and Stanford Computer Science. Licensed under Creative Commons Attribution 2.5 License. All rights reserved.

Services service: A background task used by an app. Example: Google Play Music plays the music using a service. Example: Web browser runs a downloader service to retrieve a file. Useful for long-running tasks, and/or providing functionality that can be used by other applications. Android has two kinds of services: standard services: For longer jobs; remains running after app closes. intent services: For shorter jobs; app launches them via intents. When/if the service is done doing work, it can broadcast this information to any receivers who are listening.

The service lifecycle A service is started by an app's activity using an intent. Service operation modes: start: The service keeps running until it is manually stopped. we'll use this one bind: The service keeps running until no "bound" apps are left. Services have similar methods to activities for lifecycle events. oncreate, ondestroy

Adding a service in Android Studio right-click your project's Java package click New Service Service

Service class template public class ServiceClassName extends Service { /* this method handles a single incoming request */ @Override public int onstartcommand(intent intent, int flags, int id) { // unpack any parameters that were passed to us String value1 = intent.getstringextra("key1"); String value2 = intent.getstringextra("key2"); // do the work that the service needs to do... return START_STICKY; // stay running @Override public IBinder onbind(intent intent) { return null; // disable binding

AndroidManifest.xml changes To allow your app to use the service, add the following to your app's AndroidManifest.xml configuration: (Android Studio does this for you if you use the New Service option) the exported attribute signifies whether other apps are also allowed to use the service (true=yes, false=no) note that you must write a dot (. ) before the class name below! <application...> <service android:name=".serviceclassname" android:enabled="true" android:exported="false" />

Starting a service In your Activity class: Intent intent = new Intent(this, ServiceClassName.class); intent.putextra("key1", "value1"); intent.putextra("key2", "value2"); startservice(intent); // not startactivity! or if the same code is launched from a fragment: Intent intent = new Intent(getActivity(), ServiceClassName.class);...

Intent actions Often a service has several "actions" or commands it can perform. Example: A music player service can play, stop, pause,... Example: A chat service can send, receive,... Android implements this with set/getaction methods in Intent. In your Activity class: Intent intent = new Intent(this, ServiceClassName.class); intent.setaction("some constant string"); intent.putextra("key1", "value1"); startservice(intent); In your Service class: String action = intent.getaction(); if (action == "some constant string") {... else {...

Broadcasting a result When a service has completed a task, it can notify the app by "sending a broadcast" which the app can listen for: As before, set an action in the intent to distinguish different kinds of results. public class ServiceClassName extends Service { @Override public int onstartcommand(intent tent, int flags, int id) { // do the work that the service needs to do...... // broadcast that the work is done Intent done = new Intent(); done.setaction("action"); done.putextra("key1", value1);... sendbroadcast(done); return START_STICKY; // stay running

Receiving a broadcast Your activity can hear broadcasts using a BroadcastReceiver. Extend BroadcastReceiver with the code to handle the message. Any extra parameters in the message come from the service's intent. public class ActivityClassName extends Activity {... private class ReceiverClassName extends BroadcastReceiver { @Override public void onreceive(context context, Intent intent) { // handle the received broadcast message...

Listening for broadcasts Set up your activity to be notified when certain broadcast actions occur. You must pass an intent filter specifying the action(s) of interest. public class ActivityClassName extends Activity { @Override protected void oncreate(bundle savedinstancestate) {... IntentFilter filter = new IntentFilter(); filter.addaction("action"); registerreceiver(new ReceiverClassName(), filter);

Services and threading By default, a service lives in the same process and thread as the app that created it. This is not ideal for long-running tasks. If the service is busy, the app's UI will freeze up. Example: If the Downloader app at right tries to download a large/slow file, the radio buttons and other UI elements will not respond during the download. To make the service and app more independent and responsive, the service should handle tasks in threads.

Service with thread public class ServiceClassName extends Service { /* this method handles a single incoming request */ @Override public int onstartcommand(intent intent, int flags, int id) { // unpack any parameters that were passed to us String value1 = intent.getstringextra("key1"); Thread thread = new Thread(new Runnable() { public void run() { // do the work that the service needs to do ); thread.start(); return START_STICKY; // stay running

Android thread helper classes job (or message) queue: Common pattern in Android services. New jobs come in to the service via the app's intents. Jobs are "queued up" in some kind of structure to be processed. The thread(s) process jobs in the order they came in. As jobs finish, results are broadcast back to the app. Android provides several classes to help implement multi-threaded job/message queues: Looper, Handler, HandlerThread, AsyncTask, Loader, CursorLoader,... advantages: easier to submit/finish jobs; easier synchronization; able to be canceled; support for thread pooling; better handling of Android lifecycle issues;...

HandlerThread (link) HandlerThread : just a thread that has some internal data representing a queue of jobs to perform. Looper : Lives inside a handler thread and performs a long-running while loop that waits for jobs and processes them. (link) You can give new jobs to the handler thread to process via its looper. HandlerThread hthread = new HandlerThread("name"); hthread.start(); Looper looper = hthread.getlooper();...

Handler (link) Handler : Represents a single piece of code to handle one job in the job queue. When you construct a handler, pass the Looper of the handler thread in which the job should be executed. Submit a job to the handler by calling its post method, passing a Runnable object indicating the code to run. Handler handler = new Handler(looper); handler.post(new Runnable() { public void run() { // the code to process the job... );