CIS 470 Mobile App Development

Slides:



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

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.
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 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors.
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.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
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.
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
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.
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.
DKU-MUST Mobile ICT Education Center 10. Activity and Intent.
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.
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.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Android Application -Architecture.
Concurrency in Android
Mobile Applications (Android Programming)
CS371m - Mobile Computing Services and Broadcast Receivers
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
Lecture 7: Android Services
Android – Event Handling
Mobile Application Development BSCS-7 Lecture # 6
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Chapter 3 Internet Applications and Network Programming
Activities and Intents
Android Mobile Application Development
Chapter 3: Using Methods, Classes, and Objects
Out-of-Process Components
Android Application Development android.cs.uchicago.edu
Android Programming Lecture 9
Developing Android Services
Android Programming Lecture 8
DISTRIBUTED COMPUTING
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Threads and Multithreading
Chapter 40 Remote Method Invocation
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Activities and Intents
Chapter 46 Remote Method Invocation
CIS 470 Mobile App Development
Chapter 46 Remote Method Invocation
Out-of-Process Components
Service Services.
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
SE4S701 Mobile Application Development
Mobile Programming Dr. Mohsin Ali Memon.
Activities and Fragments
Android Development Tools
CSE 451: Operating Systems Messaging and Remote Procedure Call (RPC)
CIS 470 Mobile App Development
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
CIS 694/EEC 693 Android Sensor Programming
CIS 470 Mobile App Development
Presentation transcript:

CIS 470 Mobile App Development Lecture 16 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University wenbing@ieee.org 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Services Bound service 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Bound Service A bound service is the server in a client-server interface It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC) A bound service typically lives only while it serves another application component and does not run in the background indefinitely 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Bound Service A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it To provide binding for a service, you must implement the onBind() callback method This method returns an IBinder object that defines the programming interface that clients can use to interact with the service 9/19/2019 CIS 470: Mobile App Development

Binding to a started service You can create a service that is both started and bound: you can start a service by calling startService(), which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService() Although you usually implement either onBind() or onStartCommand(), it's sometimes necessary to implement both For example, a music player might find it useful to allow its service to run indefinitely and also provide binding. This way, an activity can start the service to play some music and the music continues to play even if the user leaves the application. Then, when the user returns to the application, the activity can bind to the service to regain control of playback. 9/19/2019 CIS 470: Mobile App Development

Binding to a started service A client binds to a service by calling bindService() When it does, it must provide an implementation of ServiceConnection, which monitors the connection with the service The return value of bindService() indicates whether the requested service exists and whether the client is permitted access to it When the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection The onServiceConnected() method includes an IBinder argument, which the client then uses to communicate with the bound service 9/19/2019 CIS 470: Mobile App Development

Binding to a started service You can connect multiple clients to a service simultaneously However, the system caches the IBinder service communication channel In other words, the system calls the service's onBind() method to generate the IBinder only when the first client binds The system then delivers that same IBinder to all additional clients that bind to that same service, without calling onBind() again 9/19/2019 CIS 470: Mobile App Development

Binding to a started service The most important part of your bound service implementation is defining the interface that your onBind() callback method returns There are several different ways that you can define your service's IBinder interface 9/19/2019 CIS 470: Mobile App Development

Bound Service Lifecycle 9/19/2019 CIS 470: Mobile App Development

Bound Service Lifecycle When a service is unbound from all clients, the Android system destroys it, unless it was also started with onStartCommand() As such, you don't have to manage the lifecycle of your service if it's purely a bound service—the Android system manages it for you based on whether it is bound to any clients However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service 9/19/2019 CIS 470: Mobile App Development

Bound Service Lifecycle Additionally, if your service is started and accepts binding, then when the system calls your onUnbind() method, you can optionally return true if you would like to receive a call to onRebind() the next time a client binds to the service onRebind() returns void, but the client still receives the IBinder in its onServiceConnected() callback 9/19/2019 CIS 470: Mobile App Development

Creating a bound service When creating a service that provides binding, you must provide an IBinder that provides the programming interface that clients can use to interact with the service There are three ways you can define the interface: Extending the Binder class Using a Messenger Using Android Interface Definition Language (AIDL) 9/19/2019 CIS 470: Mobile App Development

Extending the Binder class If your service is private to your own application and runs in the same process as the client, you should create your interface by extending the Binder class and returning an instance of it from onBind() The client receives the Binder and can use it to directly access public methods available in either the Binder implementation or the Service This is the preferred technique when your service is merely a background worker for your own application The only reason you would not create your interface this way is because your service is used by other applications or across separate processes. 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Using a Messenger If you need your interface to work across different processes, you can create an interface for the service with a Messenger In this manner, the service defines a Handler that responds to different types of Message objects This Handler is the basis for a Messenger that can then share an IBinder with the client, allowing the client to send commands to the service using Message objects Additionally, the client can define a Messenger of its own, so the service can send messages back This is the simplest way to perform interprocess communication (IPC), because the Messenger queues all requests into a single thread so that you don't have to design your service to be thread-safe 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Using AIDL AIDL decomposes objects into primitives that the operating system can understand and marshals them across processes to perform IPC The previous technique, using a Messenger, is actually based on AIDL as its underlying structure As mentioned above, the Messenger creates a queue of all the client requests in a single thread, so the service receives requests one at a time If, however, you want your service to handle multiple requests simultaneously, then you can use AIDL directly. In this case, your service must be thread-safe and capable of multi-threading To use AIDL directly, you must create an .aidl file that defines the programming interface 9/19/2019 CIS 470: Mobile App Development

Extending the Binder class In your service, create an instance of Binder that does one of the following: Contains public methods that the client can call. Returns the current Service instance, which has public methods the client can call. Returns an instance of another class hosted by the service with public methods the client can call. Return this instance of Binder from the onBind() callback method In the client, receive the Binder from the onServiceConnected() callback method and make calls to the bound service using the methods provided 9/19/2019 CIS 470: Mobile App Development

Extending the Binder class public class LocalService extends Service { // Binder given to clients private final IBinder binder = new LocalBinder(); // Random number generator private final Random mGenerator = new Random(); /** * Class used for the client Binder. Because we know this service always * runs in the same process as its clients, we don't need to deal with IPC. */ public class LocalBinder extends Binder { LocalService getService() { // Return this instance of LocalService so clients can call public methods return LocalService.this; } @Override public IBinder onBind(Intent intent) { return binder; /** method for clients */ public int getRandomNumber() { return mGenerator.nextInt(100); Extending the Binder class The LocalBinder provides the getService() method for clients to retrieve the current instance of LocalService. This allows clients to call public methods in the service. For example, clients can call getRandomNumber() from the service. 9/19/2019 CIS 470: Mobile App Development

Extending the Binder class public class BindingActivity extends Activity { LocalService mService; boolean mBound = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } protected void onStart() { super.onStart(); // Bind to LocalService Intent intent = new Intent(this, LocalService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); protected void onStop() { super.onStop(); unbindService(connection); mBound = false; Extending the Binder class 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development /** Called when a button is clicked (the button in the layout file attaches to * this method with the android:onClick attribute) */ public void onButtonClick(View v) { if (mBound) { // Call a method from the LocalService. // However, if this call were something that might hang, then this request should // occur in a separate thread to avoid slowing down the activity performance. int num = mService.getRandomNumber(); Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show(); } /** Defines callbacks for service binding, passed to bindService() */ private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { // We've bound to LocalService, cast the IBinder and get LocalService instance LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; public void onServiceDisconnected(ComponentName arg0) { mBound = false; }; 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Binding to a Service Application components (clients) can bind to a service by calling bindService(). The Android system then calls the service's onBind() method, which returns an IBinder for interacting with the service The binding is asynchronous, and bindService() returns immediately without returning the IBinder to the client To receive the IBinder, the client must create an instance of ServiceConnection and pass it to bindService() The ServiceConnection includes a callback method that the system calls to deliver the IBinder 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Binding to a Service To bind to a service from your client, follow these steps: Implement ServiceConnection. Your implementation must override two callback methods: onServiceConnected(): The system calls this to deliver the IBinder returned by the service's onBind() method. onServiceDisconnected(): The Android system calls this when the connection to the service is unexpectedly lost, such as when the service has crashed or has been killed. This is not called when the client unbinds Call bindService(), passing ServiceConnection implementation When the system calls your onServiceConnected() callback method, you can begin making calls to the service, using the methods defined by the interface To disconnect from the service, call unbindService() 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Binding to a Service LocalService mService; private ServiceConnection mConnection = new ServiceConnection() { // Called when the connection with the service is established public void onServiceConnected(ComponentName className, IBinder service) { // Because we have bound to an explicit // service that is running in our own process, we can // cast its IBinder to a concrete class and directly access it. LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; } // Called when the connection with the service disconnects unexpectedly public void onServiceDisconnected(ComponentName className) { Log.e(TAG, "onServiceDisconnected"); mBound = false; }; 9/19/2019 CIS 470: Mobile App Development

CIS 470: Mobile App Development Binding to a Service With this ServiceConnection, the client can bind to a service by passing it to bindService(), as shown in the following example: Intent intent = new Intent(this, LocalService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); The first parameter of bindService() is an Intent that explicitly names the service to bind. The second parameter is the ServiceConnection object. The third parameter is a flag indicating options for the binding. It should usually be BIND_AUTO_CREATE in order to create the service if it's not already alive. Other possible values are BIND_DEBUG_UNBIND and BIND_NOT_FOREGROUND, or 0 for none. 9/19/2019 CIS 470: Mobile App Development