Background Tasks
Lesson 7
Android Developer Fundamentals V2
1
1
1
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
7.4 Services
2
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Contents
3
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Services is an advanced topic
4
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Services for Long Tasks
5
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
What is a service?
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
6
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
What are services good for?
7
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Characteristics of services
8
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Forms of services: started
9
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Forms of services: bound
10
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Services and threads
Although services are separate from the UI, they still run on the main thread by default (except IntentService)
Offload CPU-intensive work to a separate thread within the service
11
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Updating the app
If the service can't access the UI, how do you update the app to show the results?
Use a broadcast receiver!
12
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Foreground services
Runs in the background but requires that the user is actively aware it exists—e.g. music player using music service
13
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Background services limitations
14
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Creating a service
15
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Stopping a service
16
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
IntentService
17
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
IntentService
18
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
IntentService Limitations
19
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
IntentService restrictions
20
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
IntentService Implementation
public class HelloIntentService extends IntentService {
public HelloIntentService() { super("HelloIntentService");}
@Override
protected void onHandleIntent(Intent intent) {
try {
// Do some work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} // When this method returns, IntentService stops the service, as appropriate.
}
21
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Learn more
22
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
What's Next?
23
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
END
24
Services
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.