 Hello, everyone. So today in this video, we are going to see service in Android. I am Professor Dattadre Ganwal from Computer Science and Engineering Department, Vajran Institute of Technology, Salapur. So what we are going to see in this video? So after watching this video, students will be able to explain and identify different types of service and write code to create service in Android. They will be able to write code and they will be able to understand what are different types of service. First of all, we will like to introduce what is service? Service is an application component that can perform long running operations in the background and it does not provide a user interface. What is the meaning of it? So service is mostly used for running long jobs. If I suppose I want to download a big file, so that can be put it into a service. Suppose a file is very big and if I make it as an activity, what will happen? That particular file will be downloaded only when a UI is visible. But what is a drawback with such kind of practice is that you have to always keep your UI open. So it doesn't allow you to download if a UI is not in an active mode. Therefore service helps us in such situations. There are a lot of examples like if you are downloading a file from a Gmail, you are playing some music or you want to download a music file or video file. So you know that it will take a lot of time to download such files. So to avoid such UI interface and to make it active even in the background is what service helps us to do. Then it can be started by any other application or a component. So services can be started anytime while you are active with your activity part. So when you are using your activity at that particular point of time, you can start your service at any point of time. It can still be running even if the application is killed. This is one of the important features. Even if you kill an application, you can see that some files are getting downloaded in the background. You can see an arrow mark on your mobile phone which is still in download process. So if you want to kill a downloading file, then you have to go and kill that downloading file itself. That means when you kill the downloading file, it means you are killing the service. So service killing requires an explicitly it has to be done on that particular download option, not in an activity. So even if your app is not working or if you have ended your activity, still the download process will be in the working state until and unless you kill it. Android.app.service is a subclass of context wrapper. So how you will work? I will create the object of service. This particular service is present in inside context wrapper class. So context wrapper class is a root class for this particular service. This is a important part to understand and keep this particular thing in your mind. What are the different types of service? There are mostly three important types of service. Foreground, background and bound service. What is the example of foreground? Foreground is nothing but when an activity is running at that particular point of time, if a service is running, that is a foreground application. Example, an audio app. When you're listening to a music, you'll see like Ghana.com. Any other audio application when you're running, that music is getting played. When your music is getting played, that means what a service is running. Which type of service? Audio service. That song, song, particular song is audible to you because it is getting downloaded somewhere, right? It is taking some kind of your bandwidth, right? Internet, it is taking your plan. It is consuming. That means what it is running. It is downloading. You're listening to music means what it is getting downloaded in foreground. What is the meaning of background? If an app used to service to compacted storage, that would like if you're downloading a file from Gmail or you're downloading a particular music file, there is a download option sometimes in when you're listening to music. So that particular download options will work in background. Why we always try to put service in background is so that it will not disturb the running applications. I want to do surfing internet. I want to do play games and still background in background, some file get gets downloaded that is possible only with help of background service. Is that clear? Then third type is bound service in bound service is mostly applicable in client server communication. So if a particular client wants to communicate with a server, it will first of all send a bound service or it want to bind. So when it sends a bind request to server, server sees that, okay, I have enough space now, I can provide service to this particular client. If it is, it can provide, then it will say, okay, accepted your request is accepted. And then communication will take place with inter process communication IPC. Okay, and bind service will be in the running state. So this is all about services. This is an example definition of service. And this is the various important types of services. Now, next important thing is how to start a service. Now we have seen what is service and what are different types of service. Now we have to see what how to start that particular service. So there are two ways to start a service. One is start service, or you can bind a service. These are two major functions used. So a service is started when an application components such as activity starts it by calling start service. So if I want to start a service, I will call this method start service. By calling this start service method, you will be able to start a particular service. Now what how you will do with bind service? Bounds bound is nothing but you want a client want to connect to a server. When a client want to connect to a server, it will send a request called as bind services. It will call this particular method bind service. When it calls a bind service, the server will accept it and if it is available, it will accept and process it. So this is how two ways to start a service. One is start service and another is bind service. It is very easy. You have to remember only these two things start service or bind service. Now again service is having a life cycle. It is having its own life cycle. There are two. One is unbounded service and another is bounded service. Unbounded service is nothing but this one started service using start service you can do with unbounded service. Another is bound service. In bound service is second one this side. The second one is a bounded service and this is unbounded service. What is the difference in bounded service and unbounded service? In unbounded service, there is no client server type of request. Here only it will request for some downloading file. It will download as a particular file and here it is a client server communication type. Here you can see that on create there is on start command. Immediately a service is requested. It will call a method called as on start command and inside it service will be in running state. Until and unless I kill it, if I kill it, it will be on destroy and it will terminate. This is simple unbounded service. Whereas in bounded service, what it will do? On create and on bind. It has to send a bind request. That is the difference between unbounded and bounded. Here it will send a bind request and clients are bounded to a particular service. That is it will connect to a server. And once the request is accepted, clients are in running state and after completing client after completing its task, it has to call unbind. It has to unbind if a client doesn't do that after a periodic amount of time server will do it. Server will call on unbind and it will destroy a particular client. So this is how resources are allocated from server side and they are deallocated. This is about bounded and unbounded. From this particular picture, it will be it should be very clear the difference between unbounded and bounded. So there is a small question for you people. What are the two ways to start a service? We have just now see just now seen this what are two ways to start a service? Just recall it and try to answer this you can pause here and answer it. Yes, can pause it and start give the answer. So there are two types. One is start service. Another is bind service. These two methods are used to start a service. Now creating a custom service. How do you create a custom service? This is a simple snapshot to understand a pseudo this is a pseudo code I have given for your understanding. How is a service method return? So here you can see that make text and your services destroyed. This is service destroyed part. Okay, and here is make text. There is service started part. So on create the service will be started and on destroy when on destroy will be called here in the diagram you might have seen here just on destroy. So this on destroy service should be destroyed. So here on destroy service will be destroyed. This is a code you can note down this code you can pause and note down this particular code and use it. Okay, then declaring service in manifest. So again in manifest file, Android dot manifest file, you have to add this service this particular line service Android name my service. So you have to add it explicitly in manifest file we have seen manifest file earlier. So in manifest file, we will add this my service until unless you add it service won't start. It is just explicitly naming that okay, I'm going to work with services. Start and stop service you can have two buttons and in on one button I'm doing this on one button I'm starting a service on another button I am stopping a service. So start service intent you have to pass an intent we have seen intent in the previous lecture, previous video that intents are used to communication purpose and transfer data right. So here start service intent it will start a service and stop service it will destroy it will call a destroy method. So this is how a GUI will look like start service and stop service. Okay, I think it should very clear you can write this start service code here on button click and stop service here again and that button click. Okay. These are some of the references which I've referred you can go through it for more details. Okay. Thank you.