You have learned how to create and register a service that’s designed to be bound to an activity. Now, learn about the code that binds to the service and calls its methods. The first step is to create a field in the client activity type as a service. Then, create a service connection field that includes code to manage the events that happen when the service is connected and disconnected.
- [Instructor] I previously showed how to…create and register a bound service.…Now I'll show the code that binds to the service…from the activity, and calls its methods.…The first step in the activity is to add a field…that represents the service itself,…and this is just an instance of the service class.…I'll make it a private field typed as MyService,…and I'll call it mService.…Next I need something called a service connection.…And once again, this will be a field of the activity.…
This will start with private and final,…the type will be ServiceConnection,…and I'll call it mServiceConn.…I'll instantiate it with the no arguments constructor.…And that expands to implement two required methods:…onServiceConnected, and onServiceDisconnected.…I'll complete this statement by…adding the semicolon down here,…and then I'll come back to these methods and implement them.…When I bind to the service, that will cause the service…to start up, and then when it's fully connected,…the onServiceConnected method will be called.…
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