code 42 cannot connect to its background service This is a topic that many people are looking for. thetruthaboutdow.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, thetruthaboutdow.org would like to introduce to you JobIntentService – Android Studio Tutorial. Following along are instructions in the video below:
“Guys welcome to cording and flow in this tutorial. We will learn how to use use the job intent service class in android their job intent service combines two different of services. The intent service and the job service. But why is necessary since android oreo or api level 26.
We can t keep normal services running in the background anymore. Because the system will kill them after around 1 minute or even throw an exception. If we tried to start the service while our app itself as in the background this happens to save system resources. And don t use up the device s battery.
So quickly. The intent service is just a subclass of the normal service that executes. All the incoming work sequentially on the background thread. And when it s finished executing.
All the work it automatically stops itself and as a service. It is also affected by these background execution limits now one way to circumvent these restrictions is to promote the intent service to a foreground service on aurilla onwards. Which is what we do in my intent service video you can find this video. When you click on the little i in the top right corner by promoting the intent service to the foreground.
We can keep it running pretty much without any restrictions by this also means that we have to display a persistent notification that stays there for the whole duration that the service is running. Which is really intrusive. But this is supposed to show the user that there s something going on in the background. You should only use this approach.
If your service. Really must get executed immediately and without any interruptions in most cases. You should know use the job intent service. Instead.
Which uses a different approach on priore o devices. Which is api level 25. And lower it starts the normal intent service. Because there we don t have these restrictions on oreo onwards.
It uses the job schedule instead to achieve a similar behavior sn 10 service. It starts to work as soon as possible it executes either incoming intent sequentially one after another on the background thread and when it s finished processing. All the work it automatically stops itself. I have a separate video under job scheduler.
Which you can also find in the top right corner. But most of the staff reconfigured a manually is handled internally in the job intent service. So it s not quite the same these schedule jobs are a bit more reluctant than normal services. And they are also more likely to get effort and interrupted in low memory situations.
When the device files into or simply. Because they have been burning for too long. But they also save system resources. They don t need an ongoing notification like a foreign service and when they get interrupted.
They start again at a later time with the same work resent. There so if you previously used intense services in europe..
Then you should most likely convert them to a job in ten services. So let s see how we can create and run such a job in ten service for this i created a new android studio project and have already made some preparations. I have already created this layout with an edit text field and a button. I have assigned this edit text field in our main activity refined view by idea and when we click our button.
Because this method here which right now is empty. If you want you can pause. The video and type it out. But i will also put a link to the code of this example into the description box below and when we start our job intend service.
We can send data to it and this is what we will use as edit text field for in a real app. You would send data. There that you actually used to a process background work like a url for example. But since we faked the background work in this video.
We will just send some user input there so now let s create our job intentservice class for this we go into our package. We right click on the all package and create a newer java class. I m going to call it example drop and 10 servers and it has to extend job in 10 servers. This one here don t change anything else and click ok we immediately get a warning because we have to implement one method.
So we click on this line. Then we click on the little red light pipe. Implement methods we keep on hindenburg selected and click ok on hindenburg is the equivalent to one handle intent in anomaly intentservice. But before we filled this method.
We will create one constant. We write p. Rsf enter. Which has a life template for a private static final string.
Recolor tech. Equals and here we use our class name in form of a string since we just do some fake beck won t work. We will use log messages to see that everything is working properly. And that s what this tag is for ok back into our on handle work method.
This is where we do our background rock. And the same as in an intensivist this method will be executed sequentially for each intent. We send over first of i when this method starts with a shore lock message. So we type lock dear.
It automatically inserts our tag and it will simply zaiah on enter work. So we can see that this is executed and over this intent. We can send data to our service. When we started in our case.
This will be our input string from the edit text field and we get it with intent dot get string extra and then we have to pass a name. Which will be a import extra later when we had started service from our main activity. We have to use the same kea to send our input over and this is how we read it out of this intent then we will simulate work simply by freezing this thread for a couple seconds. So recreated for i loop by typing in y.
Which was another life template enter. We press tab and type in 10..
We will go through this loop for 10 rounds and in each round. We will show a lock message again. But this time it will be a the input string with and over then we append a string with a like this and i were just the current round. Then we simply cast system collected sleep freezes a thread for 1000.
Milliseconds or one second. We re run in both cases. When it s executed as an intentservice antoinette s executed as a job service and in both cases. It will run on a background thread automatically without us having to create our own the red.
Okay and the same as an enormous service. We can overwrite on create and on destroy. But we won t do anything in here. Besides showing another lock message and create and undestroyed in the intensivist video.
We acquired a wakelock and uncreate to keep the cpu of the device running even if the screen has turned off the job intent service takes care of this automatically. So we don t have to do this again in both cases as a normal intent service. And as a job service. The wakelock will be held automatically and there s another method we can overwrite.
Which is unstop current work and this method will be triggered when the current job has been stopped if it s using the job scheduler. So on api level. 26. Onwards this happens for example.
When the devices under strong memory pressure when it falls into dolls or simply. When it has been running too long because jobs have a time limit. Which is around 10 minutes after they they will get stopped and deferred and the return value. It s a boolean which defines everyone to resume our work later what just robert the default value of the superclass implementation is true.
Which means that if this job had to be stopped. It will later start again with the same intended was possessing so nothing gets lost. If we return false here the current intent and all following intents will be dropped. So the job is basically canceled.
So in most cases you are want to equip the default value. Which is true. And i will put a lock message in here as well even though i won t trigger this method in this video. But if you want to experiment with it you can see when this method is actually triggered now when the job has been stopped it doesn t automatically stop executing the on nd work method.
We actually have to take care of this hours. Earths. If we don t stop this method from executing. The system will just kill our service.
So it s probably better to stop it in a controlled manner on our own terms and the job will also release the wakelock after it has been stopped. Which could lead to a misbehave so after the job was stopped. We should stop our unending work method as well and if you have watched my job service video. Then you will probably remember that we used the boolean variable del to figure out the fish would stop our work and here we do the same in the for loop.
But we don t have to create our own boolean variable instead we have this is stopped method. Which returns true..
If the job had been stopped and ran through on stop current work and if this is true we know that we should stop our work in this case. We will simply leave this method by calling return. But if we return true a non stop current work. The job will run again at a later time with the same intent so we can resume from there.
Okay knives. And we are almost done to actually start the service. We have aesthetic method available. Which is called nq work.
Where we have to pass a context. The name of the job intent service class. A unique job. Idea and the intent.
We used to started now. We already know the class name it does of course our example job intent service class. And the job id has to be the same for either intense reasons to the service. So these two values are fixed and for this reason.
Instead of calling this nq work method from the main activity later we wrap it into our own method. So we don t put us in oncreate. This was just to show you the method outside of uncreate in this class. Rewrite static void.
And we call. It the same nq work. But here we only pass a context and intend which week our work method rubicon the nq work method you just saw a moment before where we forward our context for the class. We had called example job intensive asset class.
We also hard code the job idea for example 1 2. 3. And then 10th it s the work in 10 degrees. And over we simply cut his anchor work method from wherever we want to start our job in 10 servers in this case.
It s from our many activities. So let s switch over to our main activity job. Our fire in our nq work button method. We first want to get the input from the edit text so.
We create a string input. Equals. Edit x. Input get textual string.
Then we have to create an intent let s call it service intent equals new intent. He repairs this for the context and the class we want to start which of course our example job intend service dot class then to send our input over we take the service in tantra river carter put extra and he repairs the same string key as we defined before which was input extra and our input string as the actual value and then we use our static nq work method to rewrite example job intent service with capital year since it s a static method. We colored on the class directly ax dot. Nq work and then we simply pass a context.
This and our service intent and if you have watched my jobscheduler video. Then you know that we did a lot of configuration..
There we defined specifically under which circumstances we wanted to start our job for example. When we have wi fi connection or wendy device is currently charging. But with a job intent service. We can set these constraints instead it will basically just start as soon as possible because the point of the job intent service is to mimic the behavior of the nominal ten servers.
If you want to set these constraints like repair connection. Then the job intent service is not the correct class to use here. And here we are already done for all main activity. Lastly we go into our androidmanifestxml file have to add the permission at the top opening angle bracket uses permission and we need a wakelock permission because as i said the system will automatically keep the regular files.
But on api level 25 and lower we need this permission the same as we need to did in our intent service video. And then we have to register wall service. We do this below this activity closing tag opening angle bracket service exemplar job in 10 servers. And then important we have to add the permission here so before we close this tag in a new line rewrite android permission and he repairs android dot permission dot any capital adele s bind underscore job underscore service and then we closes with a slash and a closing angle bracket.
The job service permission. Which the job scheduler needs and since our job. Intend service runs as a job service on api level. 26.
And higher. We need a job service permission otherwise you will get a crash on aurilla onwards. But now we are done so let s test it so i ve started there and lochhead. This is an emulator that runs api level 23.
So it s below a real and now i m going to send three intents a a beer a busier. We can see on create on a network note. The first intent is running through its loop. With the input.
A and ronettes finish. We can see on enter drug again and now our a be intent and train this one finished the last one runs and then we can see ondestroy so the service is not destroyed or recreated. Again with each intent instead they are get executed sequentially one after another and then the service automatically stops itself. Because this is basically just a normal intense service.
Now let s run it on an api level above a real this is api level 28. So this time it should run as i drop a a bia abca and we can see it looks pretty much the same on create on handle work it run through its loop. If we are not in low memory situations. And the devices not endorse.
It basically runs immediately just like a normal intense service. But in some situations. This will get deferred and take a bit longer than a normal service. But in most cases.
The job intensivist should be what you need if you used intend services before so again. You can find a link to the code for this example. In the description box below and play around with it and if this video was helpful please leave a like take care. ” .
Thank you for watching all the articles on the topic JobIntentService – Android Studio Tutorial. All shares of thetruthaboutdow.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.