/Services DEMO (Android Development Fundamentals, Unit 3: Lesson 7.4)

Services DEMO (Android Development Fundamentals, Unit 3: Lesson 7.4)

Video: Services DEMO (Android Development Fundamentals, Unit 3: Lesson 7.4)

Subtitles

[MUSIC PLAYING] SPEAKER: Hi, welcome back. So now that you have understood the concept of services, we're going to look at a small example on the same. I'm going to create a simple demo by calling it Services Demo. I'm going to use 15 API, an empty activity. Name the main activity whatever you want. Now, in this particular example, there will be two buttons– Start Service and Stop Service. When you click on Start Service, your service gets started. And if you try to click again to your Start Service, it won't get started because it's already started. Now, there will be a Stop Service because we are going to implement only started types of service.

We are not going to implement the bound service. So started services run indefinitely. You've got to stop them on your own or there should be a self-stop after they are complete. So for that, we have provided a Stop Service button. You click on that, your service gets stopped. Now, if you try to click on that Stop Service again, nothing will happen because your service has already stopped. So let's just see this in action. For that, I will create a nice UI. I will drag and drop a text view and I will call it Services Demo. And 30 SP. Great, so I will create two buttons. First button– call it Start Service. Second button let me call it Stop Service. Great, so when you click on Start Service in the XML, you need to put an on click attribute and let me just call it start something. That's the method I want to implement in my activity again. And for this also, stop something.

Great, so I need to implement this method there, so I will just do Alt plus Enter, and there you go and then Enter again. Alt plus Enter, Enter again. So I have two methods here. Beautiful. All I need now is to create a service. So either I create a manually Java class and extend it by service and then put the registration or put the definition in the manifest file manually, or there is an easier way to do that. Just right click on your development package. Go to New and then go to your service, and from there select the service. Now, put the name of the service, let's say, My Own Service, whatever the name you want to give. And you want it to be exported so that others can use it or enable it. It's totally upon you if you want this always to be used by others. Check on Export or Enable, because you want to enable it.

READ  Android Tutorial #2 - History of Android

Just finish. And as you can see, my service has been created by extending the class from service. And by default, on bind method is coming here. That is for bind services. But as of right now, I'm not going to use any bind service related task, so I can return null too. Great. But what I'm going to do is I am going to create a startService, so to start that service, we need onStartCommand method. That's first. And then because I also want to show the user that service has destroyed, so I do want destroyed method to be overrided too. So if you remember the lifecycle of your started services, it starts from onCreate. Then it goes to your onStartCommand. Whenever somebody calls your startService, then service starts running and then it goes to your destroying lab second, and it gets destroyed by calling onDestroy. OK, cool. So now I have my services ready. All I need to do now is whatever the message I want to pass.

So now, inside onStart method, this is where you will put your functionality or the long running code which you want to put in the service. But if your code is taking too long, then I would suggest you create another background thread and put that code in there. And whenever it is needed, use broadcast receiver to get your UI updated. Well, why is that? Because your service, it still runs in your main thread, so you do not want to halt your main thread for too long. So tools.maketext, put the context object this. Now, services does have a context object, so you can put this. What's the message you want to show? Let's say, Service Started. S-T-A-R-T-E-D. Service Started, and with the length short .show.

READ  Annoying Orange vs Ms. Pac-Man

Great, if you want to put the log too, go on. I don't mind. So let's just put a log, My Service. Put the log services, service has started. Cool. All right, so at the end, make sure you are giving integer value. What this integer value is if you are creating services that you want even your service abruptly canceled or get destroyed next time you come, it's, again, start from there wherever you left. So we have some constant for that. So as of right now, I'm going to use START_STICKY one, so it will remain there. If you do not want START_STICKY, you can use the other constants too. And in destroy– onDestroy, again, I will just copy these two statements, and I will change the message. Like Services, Service Stop. And service has stopped. That's all. Great, so I have my service ready here, right? All I need to do now is call the service here. So how do I do that? Well, for that, I do call what? I do need an intent, right? So intent i1 let's say equals to new intent, and you're going to go from this to your service class. That is myownservice.class.

Great. And how do you start your service? By calling startService. Just parse your intent. Good. And if you want to stop your service, stopService and give the intent a name. Well, you haven't really initialized your intent. Just copy the same intent here and just put that stop. There you go. Great, I'll just run this. So let's just click on our Start Service button. It should show me a toast. Great, it's showing me toast that started services, service started. So if I go to you and normally enter this, there should be a log related to that too. Great, you can see that, service has started. So if I click on Start Service again– OK, service will again start, but the thing here is it will never create a new object. What do I mean by that? I will just explain in a bit, but let's just stop the service first. Service Stop, right? So the service has stopped, but if you tried to click on Service Stop again, it won't be calling that to us.

READ  Official Samsung Galaxy S7 Edge LED View Cover Case Review - Hands On

Why? Because it doesn't have a Start Service method again. So you see here– let me just explain it again. When you click on that button, Start Service, the method, the service was created from onCreate , and an object was created for Start Service. Now, there is one object right now. If you try to click on this, it won't create a new object. It will be just using that same object. So how exactly am I proving that? Because if I click on Stop Service, it should stop– it should show me those twice, but it's not showing me twice, as you can see. There you go. See? The object of services only created once, so that's the reason. I told you that onStart service will be called again and again and the service will be having a single object. If you want to really prove that, you can have onCreate here and in that onCreate, you can put these two logs. And service created. Great. Try to run this now.

Try this, service created. So I restart it, right? Again, see, service started only. It will never call created again until unless you destroy– well, service stopped and service created again and service started again. Cool. So that was a small example showing the lifecycle of services, how exactly you can use your lifecycle of services and how or where exactly we'll be putting the functionalities of services. You can design more complex examples using services by using the same lifecycle. So that was it from my side. Thank you. [MUSIC PLAYING].