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.

Slides:



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

Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
CSS216 MOBILE PROGRAMMING Android, Chapter 9 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Android Application Model (3)
CSS216 MOBILE PROGRAMMING Android, Chapter 3 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
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.
Lec 06 AsyncTask Local Services IntentService Broadcast Receivers.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
Android Development (Basics)
@2011 Mihail L. Sichitiu1 Android Introduction Application Fundamentals.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
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.
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.
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.
@2011 Mihail L. Sichitiu1 Android Introduction Platform Overview.
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
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.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
Activities and Intents. Activities Activity is a window that contains the user interface of your application,typically an application has one or more.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Android – Fragments L. Grewe.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
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.
Services 1 CS440. What is a Service?  Component that runs on background  Context.startService(), asks the system to schedule work for the service, to.
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.
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Speech Service & client(Activity) 오지영.
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
Concurrency in Android
Android Application Development 1 6 May 2018
CS371m - Mobile Computing Services and Broadcast Receivers
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
Lecture 7: Android Services
Mobile Application Development BSCS-7 Lecture # 6
MAD.
Activities and Intents
Android Mobile Application Development
The Android Activity Lifecycle
Android Application Development android.cs.uchicago.edu
Android Programming Lecture 9
Developing Android Services
Application Fundamentals
Android Topics UI Thread and Limited processing resources
CS323 Android Topics Network Basics for an Android App
HNDIT2417 Mobile Application Development
Service Services.
CIS 470 Mobile App Development
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
Application Fundamentals
Mobile Programming Dr. Mohsin Ali Memon.
CIS 470 Mobile App Development
Presentation transcript:

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 features: A facility for the application to tell the system about something it wants to be doing in the background. A facility for an application to expose some of its functionality to other applications.

You should use Android Services when you have to perform a time consuming and long task, like loading an Image, or a File, or download something for the Internet and asynchronous tasks in general. Using a Service you can let the UI Thread handle only UI tasks.

there are two types of Services: Unbounded : This type of Service is also called Started. This Service is completely independent for the Activity that called it and there is no communication or interaction between them. The Service simply starts (using startService), does its job and stops(using stopService) without the Activity noticing anything. Bounded : This type of Service offers communication and interaction between the Service and the Activity that launched it. Using Messagers and BroadcastReceivers the Activity can at any moment monitor the status or the progress of the background task that the Service is performing. Additionally the Service can get useful feedback from the Activity.

When a Service component is actually created, for either of above reasons, all that the system actually does is instantiate the component and call its onCreate() and any other appropriate callbacks on the main thread. It is up to the Service to implement these with the appropriate behavior, such as creating a secondary thread in which it does its work.

Service Lifecycle There are two reasons that a service can be run by the system. On call Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed). Then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called.

On multiple click or multiple call… multiple calls to Context.startService() do not nest Though they do result in multiple corresponding calls to onStartCommand(). How many times it is started a service will be stopped once Context.stopService() or stopSelf() is called. Services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed. START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them.

Clients can also use Context.bindService() to obtain a persistent connection to a service. This creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand() in the same way. The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established

Permissions Global access to a service can be enforced when it is declared in its manifest's tag. when using Context.startService(Intent), you can also set Intent.FLAG_GRANT_READ_URI_PERMISSION and/or Intent.FLAG_GRANT_WRITE_URI_PERMISSION on the Intent. This will grant the Service temporary access to the specific URIs in the Intent.

Process Lifecycle The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher of the following possibilities:

If the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed. If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible.(should not be killed except in low memory conditions).

If there are clients bound to the service, then the service's hosting process is never less important. A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

In short about services.. Services in Android is a component that runs in the background without any user interaction or interface. Unlike Activities services does not have any graphical interface. Services run invisibly performing long running tasks - doing Internet look ups, playing music, triggering notifications etc., Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them for resource management.

The only reason Android will stop a Service prematurely is to provide additional resources for a foreground component usually an Activity. When this happens, your Service can be configured to restart automatically.

Creating a Service Create a new class that extends Service. Below is the code snippet.

create a simple example to create, start and stop android service. create a layout file activity_main.xml, MainActivity and MyService class files. In MainActivity have just started and stopped MyService class through Intent on button click event. In MyService class display Toast messages for onCreate(), onStart(), onDestroy() events.

activity_main.xml

MainActivity.java

MyService.java

AndroidManifest.xml