SERVICES - Android Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys so I come back to new video this video will be about the service class in general and how we can create a service with it if you've watched my last video about intense services which I really recommend doing before watching this video then you know that an intense service is created by inheriting from the class intense service and the class intent service is actually a subclass of the service class which I'm going to talk about in this video the UI allowed for this video is very similar to the URL we had in the last video for intense services I just added an edit text where we can click on this send data button and send data to our running service so make sure to set it up like I did here or similar to that and then we can jump right into the video so in the last video about intense services we created this my intense service class here that inherits from intense servers and this time we will do the same but we will inherit from just service so let's go to your project package create a new cut length file class and I will call it my service make sure to select class press Enter and this class will this time inherit from service and not from intent service anymore and we also don't have to provide a name for this service as we had to do it for the intent service but what we have to do is we have to implement unbind which is a special function of the service class each service class needs to implement this on bind function but you will probably not need this very often it's just a method we create if you have multiple clients that want to connect to your service at the same time and if you don't need this behavior then we can just return now in this function so in Kotlin we can actually do this in one line so it doesn't take up much space in our class so the main difference between a normal service like we do it you know like we have it here and an intense service which we credit in the last video is that this intense service will automatically run in a separate thread so it won't block main thread but this intense service won't support multi-threading if we create a service like we have it here then this service class will support multi-threading but it will want in our main thread by default so therefore you should always manually start a thread when creating such a service here because otherwise your UI might freeze but I will also show you that that this is actually the case now let's start by creating a simple tag for our class to probably lock on debug messages well tag is equal to my service and then I want to go into the unit block and here we can just print a log message that just tells us that our service was just created in is running now here we can pass the tag and print service is running a very important function for services is on start command which we will implement here this on start command function will be executed and it is used to deliver the intent we started this service with so by over running this on start command function we can get the intent we started the service with and we can also attach data to that intent to communicate from our activity to our service to for example send commands and make our service do different different things in different situations in this example I just want to get the the data string attached to this intent so attached to the intent we started this service with because we will attach the string we will put in these edit text here and when we click on send data we will send this string to our running my service and we will just print the string here to assure to show you how we can actually send data to a running service so let's start by writing well data string and we get it by writing intent dot get string egg trap and I will call it extra data then we can check if this data string is now so if if we start the service for the first time then we won't attach any data string so in this case this data string will be null so we need to make this null check here and if it is not null if we go into this let block then we can simply print this data string and then what we have to do is we have to choose a return value for our on start command function we don't want to use this super on start command implementation instead we want to choose between three different values we can return here and each of those will affect how the system then with the Android system will treat our running service so in general our service will keep running once it is started until we stop it by ourselves but if the Android system needs resources then it might happen that the Android system kills your service and not your old kill you service and depending on what we return here in on start command will choose over how the system handles it if it kills your service so one return value is start not sticky that means if the the Android system kills your service then it won't recreate it if it has reach others again then we of course have start sticky this means if the service is killed by the system it will be recreated when possible and the less intent so the last intent pass to this on start command function won't be redeliver and instead this we credit service will be started with and now intent here so then this will be now and if we don't want this if you if you want to redeliver the last intent pass to this service class then we need to return start redeliver intent so these are the three return values we can choose from to return in this function and for this example i will just choose start sticky then we can actually go into our main activity and start our service when we click on the start button so it's very similar to what we've done in the last video button start service that set onclicklistener here we want to create an intent to start our service with pass the context and the service class we want to start my service double colon class at Java call it also on this intent object and simply call start service with it so with the intent and we also want to set TV service info dot text to service running and then we can copy this whole block here and paste it below for the stop button so this time it's button stop service set onclicklistener and in the last video for the intent service I showed you a way to stop the service by creating a singleton so we created an instance of this service in its class and then we could simply call this static stop service function here so we could call this function without creating an instance of this service this time I want to show you another possibility to stop our service which is in my opinion easier so we can just create an intent again which is directed to our my service and then instead of start service just call stop service and of course change the the text of the textview to service stopped and finally we can do the same for our button to send data so button send data and when we click on this button we want to send the string from our edit tags to our running service and this is actually the same we need to remove this textview to exchange and before we want to start the service we want to pass the edit text string to it so let's why well data string is equal to et later dot text dot to string and then we simply need to call our intent and call put extra with extra data because we called our extra like that if we take a look on our on my service class we will get the data from the key extra data and as a second parameter we want to insert our data string so the text from our edit text and then we want to start the service with this intent and what this will do if we call the start service function this will even though our service is still running so it won't restart it it will just call this on start fund on start command function with our new intent that has the data string attached and in this time this data string won't be now because we attach that extra data so this one's on start command function will go into this let block and print that lock message so the text of our edit text but from our service class so if we now go into our androidmanifest.xml and press control D to duplicate this service line here to add our my service we have to do this for every service we create in our app then we can start our app so I already opened lock it here and filtered it for my service and if we now click on start then our source class will go in the init block and print service is running and now what we can do is we can go into this edit text field and for example write hello guys click on send data and then our service will receive that intent we sent you it and print the data of it so hello guys so we can write whatever we want here and it will simply print the data it got from that edit text and as I told you this service runs in our applications main thread so to actually show you that I will add an infinite loop here in our on start command function that will block our thread so let's provide while true and don't write anything in the curly brackets so if it goes to this line it will just stay in this loop because this condition is always true and because it is in our main thread our UI should freeze so if you rerun this app and if we now click on start you can see how your eye freezes we can't we can't click on that edit text we can't click on the buttons because our service is stuck in this while loop and because of that you should always if you you should always start a separate thread in your service class if you have like complex calculations to do so we do that by writing thread oops thread open curly brackets put this raw loop in there and call that start afterwards and if we now rerun this app even though we still have that infinite while loop if we click on start our UI is not blocked anymore because that while loop is not executed in our applications main thread so we can still stop our service and restart it stop it send some data whatever we want and one last thing I want to show you is to to demonstrate you how the intentservice class and the normal service class differ if it comes to killing the service so if we take a look in the intent service class there I am explaining last video that if this on handle intent function returns so if if it executed all the commands in this function and there are no more intense to handle then the service this intent service will be stopped automatically but our normal service however will keep running once we started it until it stops itself or until we stop it from our activity with this arm stop service function here and to actually detect that we can go into our my service class and overwrite ondestroy which is called before our service is destroyed and here we can simply print a lot message lagdi has our tag service is being killed then we can rerun our app click on start service our service is running and we can minimize our app it doesn't really matter what we do here the service is still running and if we go back into our app we can still send send some data to our service and if we now click on stop service then it prints our service is being killed but we have to do it manually or the Android system has to do it so I hope this video was helpful for you if so please leave a like and comment below also if there is anything I can improve on please let me know in the comments that would be really helpful for me and have a good day see you next video bye bye
Info
Channel: Philipp Lackner
Views: 17,397
Rating: undefined out of 5
Keywords: android, android studio, services, service, background work, coding, programming, programmer, development, tutorial, kotlin, app
Id: EwwB_wZhBaw
Channel Id: undefined
Length: 14min 15sec (855 seconds)
Published: Mon Mar 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.