Services in Android - Part 1

Services in Android - Part 1 - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Services in Android - Part 1, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article (Part, article 1], article android, article in, article Services, article Tutorial, we can write this you understand. Well, happy reading.

Title : Services in Android - Part 1
link : Services in Android - Part 1

Baca juga


Services in Android - Part 1

hi guys Codetutor here and my name isAnil Deshpande in this particular video I want to talk about servicesin android.

services are one of the most very important concepts in android.

it is one of the four major componentsof androids other being the activity broadcast receiver and the contentprovider.

services are used to perform long running tasks in the background.

whenI say long-running tasks, it would be something like downloading a very largefile in the back ground and services don't have the UI becauseobviously you don't want to keep the user busy by showing download progresscontinuously.

So services run without UI.

The important word here is"background".

So in services implementation basically we will be talking about howto implement a service how to start a service how do i stop aservice.

When I say it runs in the background, does it actually run in thebackground on a separate thread? so without much further ado, let me get into a demo I will wereuse the code that i have been using in some of my other previous videos you cansee that i have two buttons here, start service and stop service and on click ofthis particular button I want to start a service.

So for thatfirst of all I have to create a service let me create a new class let me call it as MyService to create aservice this particular java class should extend a service class soextends Service class.

As you extend a class you have to override certainmethods so the first method is on start commandand then on bind then you have on destroy then also you have on startcommand as you can see the onstart method is deprecated so we will justremove this.

The most important methods are these three.

The onStartCommand isthe method that gets executed whenever you start the service how do i startthis particular service? So let me start the service on click ofthis particular start service button For that I have to create an intentto start a service.

Android recommends always using explicit intent so let me create a intent here private intent serviceIntent this serviceIntent needs to beinitialized, so service intent is equal to new Intent the first parameter is the context youcan say get application context and second parameter is the service so my service.

class and another importantthing is Service that you have created needs to be declared in themanifest file so let me open the manifest file, my service now how do i start the service, for thaton click on this particular button let me call a method start service I have to pass the intent, that isservicIntent, and another thing is you cannot a service there is no explicit way of stopping a service, once the services started the life cycle of the service has to bemanaged in the service itself what we can do is in the service insidethe onstart command you can do something like stop self, that will basically stopthe service before stop self so you can write any task here which you want to do in theservice.

right now what I will do is I will just only write a log statementwhich will log as soon as this particular method is started or serviceis started.

log dot i, i am using a constant string in this case I have aservice demo which is a constant I will lose that as a tag string andwhat I want to display here is I want to display the thread ID of the service so in on startCommand, thread id, for thatI need to get thread dot current thread, dot get ID.

and in the main activityalso i am displaying the thread ID now let us try to understand whetherwhen I start a service it runs on a separate thread, if it runs on theseparate thread this thread ID that I am displaying here in the main activity andthe service id that i am displaying in my service should be different so let us see whether it is different.

Let me run this code, as you can see the main activity thread idea is one and iwill start the service on click of that particular button on start commandexecute and you can see that thread ID is also one so when I say background it basicallymeans it doesn't have the UI, it doesn't mean by default service always runs on a differentthread, if you are doing any long-running task in the service and if you don'tmake sure that you don't run it on a separate thread of by spawning a threadthen you will end up getting application not responding error, i will put anotherlog statement here to show you how the service gets stopped.

let me re-run this, I will start the service and you can also see you are also getting the log out put asservice destroyed that's because I have written this stopSelf, and if i remove this andrerun it, start service, and you can see that the service is not stopping at all so you have to stop the service frominside the service you cannot stop the service fromanywhere else so that is why writing to stop self becomes very very important you cannot observe that this particularon start command returns a integer variable it basicallyreturns a kind of flag which determines behaviour of the service and also ihave got a method called on bind which I have not talked about these coming topicture when i'm talking about a bound service.

In the next video I will betalking about what do you mean by bound service in which case you will be usingthis and what is the behavior determined by the flag that is being returnedin this particular on start command method so stay tuned to the channel let me know what new things you want tolearn in this particular channel if you have got any doubts please write them in the comments section belowdon't forget to subscribe to the channel take care bye.



Thus Article Services in Android - Part 1

That article Services in Android - Part 1 This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Services in Android - Part 1 the link address http://svedda79.blogspot.com/2016/05/services-in-android-part-1.html

0 Response to "Services in Android - Part 1"

Post a Comment