Android – Services, started and bound services PART 1

Android Services, <span>Android – Services, started and bound services PART 1</span>, KandZ

Services are used to perform long-duration operations and/or access remote resources through background thread/process. They do not interact with the user directly but for example through status bar notifications. Services can be launched by activities via intents or by broadcast receivers. They can continue to run even if there is a runtime configuration change (screen rotation).

started service

It is launched via startService() and its lifecycle is independent of its creating component. You can pass parameters via the intent started the service. There is no need to return a result to the user. It shuts itself down when it is done processing or by calling stopService()

bound service

It is launched vis bindService() and its lifecycle is dependent on its creating component. You should use this kind of service when you want to interact with the service from other components. It is automatically destroyed when its client unbind.

Implementation steps

  • Extend the service class, directly with Service class or indirectly with IntentService class. IntentService is ideal for service concurrency that handles asynchronous requests and uses a worker thread to handle multiple requests simultaneously.
public class MyService extends Service{

...

}
  • Override the lifecycle hook methods for example onCreate(), onBind(), onStartCommand(), onDestroy(), onUnbind() etc. Some of the methods apply to all the service types and other to different type of service
  • define/implement other methods needed to implement the service
  • update AndroidManifest.xml to include the service so Android OS knows about it.
Android Services, <span>Android – Services, started and bound services PART 1</span>, KandZ

END OF PART 1

94200cookie-checkAndroid – Services, started and bound services PART 1Android – Services, started and bound services PART 1

Related posts

5 Thoughts to “Android – Services, started and bound services PART 1

  1. […] Android – Services, started and bound services PART 1 […]

  2. […] Android – Services, started and bound services PART 1 […]

Comments are closed.