You can think of a bound service as the server in a client-server style application. The user sees the activity—the user interface—while the service provides back-end processing. By default, a bound service only runs as long as its bound activity stays alive. When the activity the service is bound to shuts down, the service becomes unavailable. It might not be completely destroyed right away, but it’s no longer accessible to the application and is eligible to be shut down by the app framework.
- [Instructor] I've described how to use Intent services to run operations in the background and in many cases this is a great type of service to use for tasks that have a distinct starting and ending state. There are other kinds of operations, though, that need services that survive for longer than a single task, and that brings us to bound services. You can think of a bound service as a server in a client server style application. The user sees the activity, the user interface, while the service provides backend processing, and because the service stays alive, it can maintain its own state.
By default, a bound service only runs as long as its bound activity or fragment stays alive. When the activity the service is bound to shuts down, the service is no longer available. It might not be completely destroyed right away by the system, but it's no longer accessible to the application, and it's eligible for being shut down and destroyed. It's important to note that starting with Android 5.0, the JobScheduler API took over many of the responsibilities that were previously allocated to bound services for background processing.
And in Android 8.0 Oreo, there are some new rules about what you can and can't do with bound services. One of the troubles with bound services that run for a long time, is they can sit in the background and drain the battery. And so particularly in Android 8.0 Oreo, the use of the JobScheduler API is strongly recommended. But bound services are still important for certain operations, and they're certainly used in many existing apps, so we'll take a look at them.
Like in intent service, a bound service is implemented with a Java class. This class extends the service class, and like intent service, it has to be registered in the app manifest. I'll go to my project window to the app module, and once again, I'll create a new service, and this time I'll choose the first option simply labeled service. In this dialogue, I'm asked whether the service will be exported. I'll uncheck that option, and that means that the service will only be available to my applications process.
I'll click finish, and that creates the beginning state of my service class. I'll go to my application manifest, and show that the service has been registered there as required. The service class starts with a single override method called onBind, and a constructor method that takes no arguments. I'll leave the constructor method exactly as it is, and focus on the onBind method. The onBind method returns an implementation of an interface called IBinder.
My job is to construct that object so I can return it. I'll define a class down here, and I'll name it ServiceBinder. And I'll extend the binder class. And within that class, I'll create a new method called getService that returns an instance of my class, and I'll return the current instance of the class with return MyService.this. Next I'll go up to the top of the service class, and I'll add a new persistent field that'll be private and final, and the type will be binder, and I'll call it mBinder, and I'll get its value by calling new ServiceBinder.
And then I'll go down to the onBind method that was generated for me, and I'll return that object, mBinder. So these are the four required components. The field that returns the binder object, the onBind method that can be called from elsewhere in the application, and the class that extends the binder class. For debugging purposes, I'll use logcat output so I can trace the event when the service is created. As I've done throughout the course, I'll use a tag of CodeRunner, and I'll turn that into a constant.
And I'll give the constant a name of TAG. Then I'll add one more call to logcat. I'll override a couple of more methods that will help me show clearly the lifetime of the service. I'll override the onCreate method, and I'll add logcat output there. And then I'll do the same thing with the onDestroy method. And there's one other method that I'd like to trace, and it's called onUnbind.
And I'll add the logcat output here above the call, to the superclasses method. And that's all the code I need to create my service. So now the service is ready for use, and in the next video I'll describe how to bind to it from an activity.
First, discover how to create and start simple background threads, and how to use handlers to manage a thread's message queue. Then, learn various methods for optimizing the scheduling and performance of background tasks in Android with AsyncTask, intent services, and the JobScheduler API. Plus, explore tools that help you implement multithreading for different tasks in Android: Loader, for asynchronous data loading, and the open-source API Retrofit, for making HTTP requests.
Note: To get the most out of this course, you should be comfortable programming with Java, and should understand the most basic skills that are needed to build Android apps with the Android SDK and Android Studio.
- Creating and running a background thread
- Sending messages to the UI from threads
- Managing multiple background threads
- Managing threads with AsyncTask
- Managing long-running tasks with services
- Scheduling background tasks with JobScheduler
- Using other APIs for concurrent programming