Android Services CS 4720 Mobile Application Development Resource: - - PowerPoint PPT Presentation

android services
SMART_READER_LITE
LIVE PREVIEW

Android Services CS 4720 Mobile Application Development Resource: - - PowerPoint PPT Presentation

Android Services CS 4720 Mobile Application Development Resource: developer.android.com CS 4720 The Android Architecture CS 4720 2 The Service A Service is a process running the background It does not have a UI A Service


slide-1
SLIDE 1

CS 4720

Android Services

CS 4720 – Mobile Application Development Resource: developer.android.com

slide-2
SLIDE 2

CS 4720

The Android Architecture

2

slide-3
SLIDE 3

CS 4720

The Service

  • A Service is a process running the background
  • It does not have a UI
  • A Service can communicate with the user

through Toasts and the Status Bar

  • Services can do basically anything in the

background:

– Download/Upload Data – Listen for data changes (location, etc.) or Intents – Log information

3

slide-4
SLIDE 4

CS 4720

Service Lifecycle

4

slide-5
SLIDE 5

CS 4720

Service Lifecycle

  • A Service’s lifecycle is a bit different from an

Activity

– onCreate() – basically the same as Activity – onStartCommand() – gets the service going – onBind() – allows a Service to be a provider – onDestroy() – basically the same as Activity – stopSelf() – called by the Service to end it – startService() and stopService() – Intent calls to start and stop a Service

5

slide-6
SLIDE 6

CS 4720

Starting and Stopping

  • Works almost exactly the same as starting an

Activity with an Intent (see code example)

  • However, a Service will keep running after

Activities close

  • You MUST manage your Services! Stopping

them is necessary to not take up extra resources

6

slide-7
SLIDE 7

CS 4720

Binding

  • Consider a “normal” Service as doing a

background activity for a single Activity

  • A Service that implements onBind() is one that

is exposing some functionality for other Activities to utilize

  • onBind() is called by other Activities to access

the functionality

7

slide-8
SLIDE 8

CS 4720

In the Foreground

  • Services can be set to run in the foreground
  • Using startForeground() will start a Service that

the system knows it can’t just kill if necessary

  • The Service should provide direct access to the

user for interaction, such as the status bar

  • Example: a music player

8

slide-9
SLIDE 9

CS 4720

What to do with it?

  • What can you do with a service?
  • Example code time again!

9