Download the New LearnVern Android App and enter Lucky Draw to win Free Certificate
Now we will see our next component which is the 'Android Service' component. We call the service as background process, there are many tasks that we have to achieve in the background. Like we run any application on our mobile and the song plays in its background. So now how can we use this type of service on our Android we'll see.
For this, we have to extend the service that is its parent class, due to which we can use its inbuilt method.
In Android, a Service is used for two main reasons.
Now what is this thing and how can we apply it, we will look at this with the help of an example, suppose we have to update an application, which is a very common thing and we use many types of mobile applications on our phones. Many apps on our phones keep on updating every day and many need to get an update. So we keep a lot of applications on auto-updates, which means whenever its update is available, it gets updated automatically and there are many applications that we update manually.
There can be two forms of Android Service. The lifecycle of service can follow two different paths.
Call to startService() -> onCreate() -> onStartCommand() -> Service running [The service is stopped by itself or a client] -> onDestroy() -> Service shut down
Call to bindService() -> onCreate() -> onBind() -> Clients are bound to service [All clients unbind by calling unbindService()] -> onUnbind() -> onDestroy() -> Service shut down
We will see this concept with the help of an example of an app update, so all the mobile applications that are in our phones, such as educational apps, game applications, social media apps, what happens in all those applications is that they take data from a remote server, and as soon as that service is finished that means that as soon as that application is updated, then we will get a notification that your application has been updated.
So it became a simple way to use the 'Service'.
There are some common functions that we need to share, and the main purpose of sharing that is that we can reuse it, use that code back, a simple example that we talk can about is, suppose we have prepared a service, inside which we have written the code to send an email, and in different applications, wherever we want to send our email, we will call our service, and send the same email, that is a reuse of the code. We will call it Inter-Process-Communication. Here our service will take the form of a remote interface so that other applications can also use it.
When we talk about Bind or Unbind service, what happens in it is that sometimes we keep manual updates, so we have to bind the service itself, and then after the update, we have to unbind it when we get the update finish notification.
Lets took an Android Service Example
Have a friend to whom you would want to share this course?