Android Services
Services
• Services are components that run in background
• Exist to perform long-running operations
• Service run until it completes it work
• Lower priority the current Activity
• Services are about component lifetime not threading
• Services are component concept

2
Implement a Service
• Services are implemented by extending the Service class
• Must always provide onBind implementation
• A service has distinct lifecycle
• onCreate
• onDestroy
• onStartCommand for search Intent

• List in manifest with <services> element

3
On-Demand Services
• Easiest Service to implement is an on-demand service
• Android starts service on first request
• Service remains running as long as requests are being processed
• Service terminates as soon as requested processing completes

• Android provides an identifier for each request
• onStartCommand receives a unique startId with each request
• Indicate that request is complete by calling to stopSelfResult
• Pass startId corresponding to the request you’ve completed
• If Android has passed additional requests, Service remains running
• If that startId is from the most recent request, Service is terminated

4
Handling unexpected termination
• Android may kill Service prematurely if under resource pressure
• Android can later restart Service
• onStartCommantd return values indicate restart needs
• START_REDELIVER_INTENT
• Start service with intent

• START_STICKY
• START_NOT_STICKY

5
DEMO

6 / 11
ANY QUESTIONS?

7/11

Project a day 3 services

  • 1.
  • 2.
    Services • Services arecomponents that run in background • Exist to perform long-running operations • Service run until it completes it work • Lower priority the current Activity • Services are about component lifetime not threading • Services are component concept 2
  • 3.
    Implement a Service •Services are implemented by extending the Service class • Must always provide onBind implementation • A service has distinct lifecycle • onCreate • onDestroy • onStartCommand for search Intent • List in manifest with <services> element 3
  • 4.
    On-Demand Services • EasiestService to implement is an on-demand service • Android starts service on first request • Service remains running as long as requests are being processed • Service terminates as soon as requested processing completes • Android provides an identifier for each request • onStartCommand receives a unique startId with each request • Indicate that request is complete by calling to stopSelfResult • Pass startId corresponding to the request you’ve completed • If Android has passed additional requests, Service remains running • If that startId is from the most recent request, Service is terminated 4
  • 5.
    Handling unexpected termination •Android may kill Service prematurely if under resource pressure • Android can later restart Service • onStartCommantd return values indicate restart needs • START_REDELIVER_INTENT • Start service with intent • START_STICKY • START_NOT_STICKY 5
  • 6.
  • 7.