Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 Services

3 A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application component, such as an activity, can start a service, and it will continue to run in the background even if the user switches to another application. Example: An activity could start a service to handle network transactions, play music, or perform file I/O in the background without blocking user interaction with the activity. A service is implemented as a subclass of Service. Slide 2©SoftMoore Consulting

4 Services Versus Threads A service can run in the background even when the user is not interacting with the application. If work needs to be performed outside the main thread only while the user is interacting with the application, you should probably create a new thread and not a service. If you want to play music only while an activity is running, you could create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Slide 3©SoftMoore Consulting Caution: A service runs in the main thread of its hosting process. The service does not create its own thread and does not run in a separate process unless you specify otherwise. You should still create a new thread within the service if it performs intensive or blocking operations.

5 Forms of a Service Started: A service is “started” when an application component starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. Bound: A service is “bound” when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service. A bound service runs only as long as another application component is bound to it. Slide 4©SoftMoore Consulting Note: A service can be both started and bound.

6 Creating a Service Create a subclass of Service (or one of its existing subclasses) and override some callback methods that handle key aspects of the service lifecycle. If a component starts the service by calling startService(), then the service remains running until it stops itself with stopSelf() or another component stops it by calling stopService(). If a component starts the service by calling bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the component is bound to it. The system destroys the service once it is unbound from all clients. Slide 5©SoftMoore Consulting

7 Service Lifecycle Slide 6©SoftMoore Consulting

8 Important Service Callback Methods. onCreate() –Called by the system when the service is first created, to perform one-time setup procedures (before it calls either onBind() or onStartCommand() ). –Not called if the service is already running. onStartCommand() –Called by the system when another component (e.g., an activity) calls startService(). –Once this method executes, the service is started and can run in the background indefinitely. –If implemented, it is your responsibility to stop the service when its work is done by calling stopSelf() or stopService(). –Do not implement if you only want to provide binding. Slide 7©SoftMoore Consulting

9 Important Service Callback Methods (continued) onBind() –Called by the system when another component wants to bind with the service by calling bindService(). –Implementation must provide an interface that clients use to communicate with the service by returning an IBinder instance. –You must always implement this method, but if you don't want to allow binding, then you should return null. onDestroy() –Called this method when the service is no longer used and is being destroyed. –Cleans up any resources such as threads, registered listeners, receivers, etc. –This is the last call the service receives. Slide 8©SoftMoore Consulting

10 Service Subclasses Service : Base class for all services. –When you extend this class, it’s important to create a new thread in which to do all the service’s work, because the service uses the application’s main thread, by default. IntentService : A subclass of Service that uses a worker thread to handle all start requests, one at a time. Usually the best option if the service does not need to handle multiple requests simultaneously. Other Service subclasses –RemoteViewsService– WallpaperService –... (others) Slide 9©SoftMoore Consulting

11 IntentService Creates a default worker thread that executes all intents delivered to onStartCommand() separate from the application’s main thread. Creates a work queue that passes one intent at a time to your onHandleIntent() implementation. Stops the service after all start requests have been handled, so you never have to call stopSelf(). Provides default implementation of onBind() that returns null. Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to the onHandleIntent() implementation. Slide 10©SoftMoore Consulting

12 Creating an IntentService Provide a default constructor that calls the superclass constructor with a name for the worker thread. public HelloIntentService() { super("HelloIntentService"); } Implement method onHandleIntent(). When this method returns, IntentService stops the service. Slide 11©SoftMoore Consulting

13 Declaring a Service in the Manifest Similar to activities and other components, all services must be declared in the application’s manifest file. Add a element as a child of the element....... Note: There are other attributes you can include in the element – see references. Slide 12©SoftMoore Consulting

14 Example: Service public class MusicPlayerService extends Service { private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); //... create/show "Service created..." Toast } Slide 13©SoftMoore Consulting

15 Example: Service (continued) @Override public int onStartCommand(Intent intent, int flags, int startId) { //... create/show "Service started..." Toast Thread backgroundThread = new Thread(new Runnable() { public void run() { playSong(); } }); backgroundThread.start(); return START_NOT_STICKY; } Slide 14©SoftMoore Consulting

16 Example: Service (continued) @Override public void onDestroy() { //... create/show "Service destroyed..." Toast if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } Slide 15©SoftMoore Consulting

17 Example: Service (continued) private void playSong() { if (mediaPlayer != null) mediaPlayer.release(); mediaPlayer = MediaPlayer.create(this, R.raw.tgcb); mediaPlayer.start(); } Slide 16©SoftMoore Consulting

18 Example: Service (continued) // in the MainActivity class public void onCreate(Bundle savedInstanceState) {... Button playButton = (Button) findViewById(R.id.playButton); playButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MusicPlayerActivity.this, MusicPlayerService.class); startService(intent); } }); Slide 17©SoftMoore Consulting

19 Example: Service (continued) Button stopButton = (Button) findViewById(R.id.stopButton); stopButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MusicPlayerActivity.this, MusicPlayerService.class); stopService(intent); } }); } Slide 18©SoftMoore Consulting

20 Example: Service (continued) Slide 19©SoftMoore Consulting

21 Relevant Links Services http://developer.android.com/guide/components/services.html Bound Services http://developer.android.com/guide/components/bound-services.html Class Service http://developer.android.com/reference/android/app/Service.html Slide 20©SoftMoore Consulting


Download ppt "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."

Similar presentations


Ads by Google