1 of 24

Background Tasks

Lesson 7

Android Developer Fundamentals V2

1

1

1

Services

Android Developer Fundamentals V2

Services

Android Developer Fundamentals V2

2 of 24

7.4 Services

2

Services

Android Developer Fundamentals V2

3 of 24

Contents

  • Services for long tasks.
  • IntentService

3

Services

Android Developer Fundamentals V2

4 of 24

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.

4

Services

Android Developer Fundamentals V2

5 of 24

Services for Long Tasks

5

Services

Android Developer Fundamentals V2

6 of 24

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

7 of 24

What are services good for?

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

7

Services

Android Developer Fundamentals V2

8 of 24

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.

8

Services

Android Developer Fundamentals V2

9 of 24

Forms of services: started

  • Started with startService()
  • Runs indefinitely until it stops itself
  • Usually does not update the UI

9

Services

Android Developer Fundamentals V2

10 of 24

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

10

Services

Android Developer Fundamentals V2

11 of 24

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

12 of 24

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

13 of 24

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 service is running

13

Services

Android Developer Fundamentals V2

14 of 24

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.

14

Services

Android Developer Fundamentals V2

15 of 24

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.

15

Services

Android Developer Fundamentals V2

16 of 24

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 unbound
  • IntentService is destroyed after onHandleIntent() returns

16

Services

Android Developer Fundamentals V2

17 of 24

IntentService

17

Services

Android Developer Fundamentals V2

18 of 24

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

18

Services

Android Developer Fundamentals V2

19 of 24

IntentService Limitations

  • Cannot interact with the UI
  • Can only run one request at a time
  • Cannot be interrupted

19

Services

Android Developer Fundamentals V2

20 of 24

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.

20

Services

Android Developer Fundamentals V2

21 of 24

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

22 of 24

Learn more

22

Services

Android Developer Fundamentals V2

23 of 24

What's Next?

23

  • Concept Chapter: 7.4 Services
  • No practical

Services

Android Developer Fundamentals V2

24 of 24

END

24

Services

Android Developer Fundamentals V2