Understanding the Service Lifecycle - Android

Before we get into the details of how to create a service, let’s look at how services interact with the Android operating system. First, it should be noted that a service implementation must be registered in that application’s manifest file using the <service> tag. The service implementation might also define and enforce any permissions needed for starting, stopping, and binding to the service, as well as make specific service calls.

After it’s been implemented, an Android service can be started using the Context. startService() method. If the service was already running when the startService() method was called, these subsequent calls don’t start further instances of the service. The service continues to run until either the Context.stopService() method is called, or the service completes its tasks and stops itself using the stopSelf() method.

To connect to a service, interested applications use the Context.bindService() method to obtain a connection. If that service is not running, it is created at that time. After the connection is established, the interested applications can begin making requests of that service, if the applications have the appropriate permissions. For example, a Magic Eight Ball application might have an underlying service that can receive yes-or-no questions and provide Yoda-style answers. Any interested application could connect to the Magic Eight Ball service, ask a question (“Will my app flourish on the Android Market?”) and receive the result (“Signs point to Yes.”).The application can then disconnect from the service when finished using the Context.unbindService() method.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics