07.4 Services

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Android Developer Fundamentals V2

Background
Tasks
Lesson 7

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 1
Fundamentals V2 International License
7.4 Services

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 2
Fundamentals V2 International License
Contents

● Services for long tasks.


● IntentService

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 3
Fundamentals V2 International License
Services is an advanced topic

● Services are complex.


● Many ways of configuring a service.
● This lesson has introductory information only.
● Explore and learn for yourself if you want to use
services.

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 4
Fundamentals V2 International License
Services for
Long Tasks

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 5
Fundamentals V2 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.

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 6
Fundamentals V2 International License
What are services good for?

● Network transactions.
● Play music.
● Perform file I/O.
● Interact with a database.

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 7
Fundamentals V2 International License
Characteristics of services

● Started with an Intent.


● Can stay running when user switches applications.
● Lifecycle—which you must manage.
● Other apps can use the service—manage
permissions.
● Runs in the main thread of its hosting process.
This work is licensed under a
Android Developer Services Creative Commons Attribution 4.0 8
Fundamentals V2 International License
Forms of services: started

● Started with startService()


● Runs indefinitely until it stops
itself
● Usually does not update the UI

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 9
Fundamentals V2 International License
Forms of services: bound

● Offers a client-server interface that


allows components to interact with
the service
● Clients send requests and get results
● Started with bindService()
● Ends when all clients unbind

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 10
Fundamentals V2 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

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 11
Fundamentals V2 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!

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 12
Fundamentals V2 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
● Higher priority than background services since
user will notice its absence—unlikely to be killed
by the system
● Must provide a notification which the user cannot
dismiss while the serviceServices
Android Developer
is running
This work is licensed under a
Creative Commons Attribution 4.0 13
Fundamentals V2 International License
Background services limitations
● Starting from API 26, background app is not allowed to create a
background service.
● A foreground app, can create and run both foreground and
background services.
● When an app goes into the background, the system stops the
app's background services.
● The startService() method now throws an
IllegalStateException if an app is targeting API 26.
● These limitations don't affect foreground services or bound
services. This work is licensed under a
Android Developer Services Creative Commons Attribution 4.0 14
Fundamentals V2 International License
Creating a service
● <service android:name=".ExampleService" />
● Manage permissions.
● Subclass IntentService or Service class.
● Implement lifecycle methods.
● Start service from Activity.
● Make sure service is stoppable.
This work is licensed under a
Android Developer Services Creative Commons Attribution 4.0 15
Fundamentals V2 International License
Stopping a service
● A started service must manage its own lifecycle
● If not stopped, will keep running and consuming
resources
● The service must stop itself by calling stopSelf()
● Another component can stop it by calling
stopService()
● Bound service is destroyed when all clients
This work is licensed under a
unbound Android Developer
Fundamentals V2
Services
Creative Commons Attribution 4.0
International License
16
IntentServic
e

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 17
Fundamentals V2 International License
IntentService

● Simple service with simplified lifecycle


● Uses worker threads to fulfill requests
● Stops itself when done

● Ideal for one long task on a single background


thread
This work is licensed under a
Android Developer Services Creative Commons Attribution 4.0 18
Fundamentals V2 International License
IntentService Limitations

● Cannot interact with the UI


● Can only run one request at a time
● Cannot be interrupted

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 19
Fundamentals V2 International License
IntentService restrictions
● IntentService are subjected to the new
restrictions on background services.
● For the apps targeting API 26,
Android Support Library 26.0.0 introduces a new
JobIntentService.
● JobIntentService provides the same functionality
as IntentService but uses jobs instead of
services.
This work is licensed under a
Android Developer Services Creative Commons Attribution 4.0 20
Fundamentals V2 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.
}

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 21
Fundamentals V2 International License
Learn more

● Services overview
● Background Execution Limits

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 22
Fundamentals V2 International License
What's Next?

● Concept Chapter: 7.4 Services


● No practical

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 23
Fundamentals V2 International License
END

This work is licensed under a


Android Developer Services Creative Commons Attribution 4.0 24
Fundamentals V2 International License

You might also like