android services
play

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


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

  2. The Android Architecture CS 4720 2

  3. 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 CS 4720 3

  4. Service Lifecycle CS 4720 4

  5. 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 CS 4720 5

  6. 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 CS 4720 6

  7. 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 CS 4720 7

  8. 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 CS 4720 8

  9. What to do with it? • What can you do with a service? • Example code time again! CS 4720 9

Recommend


More recommend