Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.

Slides:



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

Cosc 4/5730 Android and Blackberry Near Field Communications (NFC)
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.
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,
Extras Plus! Pepper. Objectives extra knowledge Cookies Picture handling when creating site.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
The Internet & The World Wide Web Notes
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
Cosc 5/4730 A little on threads and Messages: Handler class.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
JavaScript & jQuery the missing manual Chapter 11
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.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
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.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
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.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
CSE 451: Operating Systems Winter 2015 Module 22 Remote Procedure Call (RPC) Mark Zbikowski Allen Center 476 © 2013 Gribble, Lazowska,
Concurrent Programming and Threads Threads Blocking a User Interface.
Services 1 CS440. What is a Service?  Component that runs on background  Context.startService(), asks the system to schedule work for the service, to.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Mobile Programming Midterm Review
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
SPI NIGHTLIES Alex Hodgkins. SPI nightlies  Build and test various software projects each night  Provide a nightlies summary page that displays all.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
Cosc 5/4735 Voice Actions Voice Interactions (API 23+)
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Speech Service & client(Activity) 오지영.
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.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Small talk with the UI thread
CS371m - Mobile Computing Services and Broadcast Receivers
Instructor: Mazhar Hussain
Notifications and Services
Chapter 5 Conclusion CIS 61.
Reactive Android Development
Android Application Development android.cs.uchicago.edu
Android Programming Lecture 9
Developing Android Services
CSE 451: Operating Systems Winter 2006 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
CSE 451: Operating Systems Autumn 2003 Lecture 16 RPC
CSE 451: Operating Systems Winter 2007 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Winter 2004 Module 19 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Autumn 2009 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Threads, Handlers, and AsyncTasks
Service Services.
CSE 451: Operating Systems Autumn 2010 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Winter 2003 Lecture 16 RPC
CSE 451: Operating Systems Messaging and Remote Procedure Call (RPC)
CIS 470 Mobile App Development
Presentation transcript:

Cosc 5/4730 Android Services

What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what it is not: – A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of. – A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). Thus 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 (even when the user is not directly interacting with the application). This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.Context.startService() – A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.Context.bindService()

What is a service? (2) Basically a service can be though of as a data structure that runs. It doesn’t need a screen – Normally communicates with the activity that started it. It can run in parallel with an activity providing data and stuff. (binding) it may run without activity, to provide data for later use. – Say an alarm starts it every X minutes to check on something. Then may use a handler, notification, call a broadcast receiver, maybe even start an activity. Or my just write out the data to local storage, for later use.

What is a service? (3) Example – Use clicks on a picture that they want to download from the web into their gallery. – The application kicks off a downloader service and then the user continues. – The service downloads the picture and puts into the gallery. And creates a Notification when done that the picture has completed (or failed). The user can click on the notification and open the picture (using the gallery app).

What is a service? (4) Service – This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running. IntentService – This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work. – You can override other methods as needed like you would need in a Service.

IntentService Easy to implement Create a constructor with a super(“name”) Override onHandleIntent(Intent) { …} – Inside is the work to be done. – The Extra bundle tells you what “to process”. – Send broadcast intent or notification when “completed”. May also send a message back to the activity via handler by putting the messenger object in the Bundle!

IntentService Example public class myIntentService extends IntentService { required constructor public myIntentService() { super(“myIntentService"); } Where we do the protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); //now get the information you need to do whatever is needed. }

Call the IntentService My service “returns” X number of random numbers based on the intent, so Intent number5 = new Intent(getBaseContext(), myIntentService.class); number5.putExtra("times", 5); //5 random number Send numbers back through a messenger Messenger messenger = new Messenger(handler); number5.putExtra("MESSENGER", messenger); If no MESSENGER key, then the service will use notifications. startService(number5); //start the service.

Service. Far more complex. – Need to create thread for it – The serviceHandler as well – Onstartcommand which will get the intent and then pass the information onto the servicehandler (via a messenger) so it off on it’s own thread. – You can also setup the IBinder as well.

Service code example. Basically, I copied the code from the developer site and then added my own to the handleMessage(Message) method. nts/services.html#ExtendingService nts/services.html#ExtendingService – You may need to initialize some things in the OnCreate() and/or OnStartCommand(…) as well.

ServiceDemo Remember a service, is just like an activity, except there is no screen. – So most of the code is downloading files and using file I/O and the notification system. Nothing really amazing at this point. – When you need a service, you start it with an intent and the startService(intent) command. Just like you would an activity. The service end when they are done and what for the next call.

Binder Android Interface Definition Language (AIDL) – similar to other IDLs if you have worked with them before. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).

Binder (2) In it’s simplest form, you allow the activity to call methods in the running service via the binder service. – Instead of startService BindService(intent, ServiceConnection, flags) Now you use the ServiceConnection variable to call into the service to retrieve data or cause the service to do something. – Hard to show in powerpoint, since there are lots of little pieces. Easier to show in code.

Manifest file. Like everything else services must be registered listed in the AndroidManifest.xml – Uses the tag and is pretty simple. – Example: <service android:name=".myIntentService“ android:enabled="true" android:exported="true">

Example code. The ServiceDemo – One example intent service for random numbers – One Example Service for random numbers You can compare the complexity of a service with an intentService – fileDlService is an intentService. It takes a URL ( to down a picture and stores in the SD card downloads directory. When completed it sends a notification, so the user can open the file in the gallery viewer.

Example code (2) ServiceDemoIPC – Uses a ServiceConnection to allow the activity to call into the service to get a random number – Based on Googles code. ServiceDemoMSG – Setups a handler to send messages to a service This service will toast a message. But you can easily change that to have the service do many difference things, based on the message.

References /app/Service.html /app/Service.html /services.html /services.html /article.html /article.html /aidl.html /aidl.html /bound-services.html /bound-services.html

Q A &