Running in a Background Service

使用IntentService

create an IntentService

RSSPullService extends IntentService
override onHandleIntent()

Define the IntentService in the Manifest

android:exported is set to "false", the service is only available to this app.

Create and Send a Work Request to an IntentService

  • Create a new Intent --- mServiceIntent
  • Call startService()
    Once you call startService(), the IntentService does the work defined in its onHandleIntent() method.

Report Status From an IntentService

  • Create an Intent --- localIntent
  • Send the Intent
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent)

Recieve Status Broadcasts from an IntentService

  • ResponseReceiver extends BroadcastReceiver
    Prevents instantiation --- DownloadStateReceiver()
    onReceive()
  • Create an IntentFilter
  • Register the BroadcastReceiver and its IntentFilter

你可能感兴趣的:(Running in a Background Service)