Course Content

Course Content

Android Components - Services

Android Service Component

 

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.

 

Why we need Android Services?

 

In Android, a Service is used for two main reasons.

  • Implement multi-task.
  • Enable Inter-Process-Communication (IPC).

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.

 

Android Service LifeCycle

 

There can be two forms of Android Service. The lifecycle of service can follow two different paths.

  1. Started
  2. Bound

1. Started Service (Unbounded Service)

Call to startService() -> onCreate() -> onStartCommand() -> Service running [The service is stopped by itself or a client] -> onDestroy() -> Service shut down

 

2. Bounded Service

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'.

 

Inter-Process-Communication (IPC)

 

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

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere