2

I want to run an long running operation in Android. Say the task would run for about 5-10 mins. For this reason I am planning to use a JobIntentService and Bind it to an Activity.

Right now I am using a AsyncTask, even though I know AsyncTask cannot/should not be used for long running operations hence I am planning to change it now. Many times what happens is while the task is running the user minimizes the app and after sometime the Android OS closes/clears the Activity to free up some memory.

So my AsyncTask is kept running without any purpose and crashes while trying to update a view in that Activity.

So I am planning to use an JobIntentService . But will using an JobIntentService and Binding it to an Activity will reduce the chances of Android OS closing/clearing the Activity? or still will it follow the same process?

Any Help would be really grateful.

Rajesh K
  • 683
  • 2
  • 9
  • 35
  • 1
    Why do you want your Activity to not be cleared? – Anatolii Jul 25 '18 at 20:55
  • @Anatolii Suppose the Activity is being cleared then the progress update to the Activity is cleared and when the users return back to the activity when the long running operation is running I want the user to see the progress update. And the other case is suppose the user return back to the app after the long running operation is over and also the activity is also cleared the what will happen to the results of the service? What can be the best way to store them? – Rajesh K Jul 25 '18 at 22:33

2 Answers2

4

If your Activity is in the background then blocking Android from killing your Activity doesn't sound like a good idea (and even necessary) to me. Since your Activity is in the background it's not visible to the user and there's no need to update the UI during this time.

Instead, you could do the following:

If Activity is in the foreground then it can receive updates from your JobIntentService via BroadcastReceiver that you register/unregister with LocalBroadcastManager in onResume/onPause.

IF JobIntentService has completed its work when your Activity was killed or in the background you could persist the result somewhere (for example, in SharedPreferences) and then in onResume of your Activity you could read that result from SharedPreferences.

Anatolii
  • 14,139
  • 4
  • 35
  • 65
  • Thanks one again for your helpful answer :). – Rajesh K Jul 26 '18 at 13:06
  • Sorry but one more question. Is it possible to stop the Service if the user himself closes the app from the recent list. But the service should be running if the user if the Android OS closes the app. – Rajesh K Jul 28 '18 at 15:12
  • 2
    1) Have `onStartCommand()` of your `Service` return `START_STICKY` 2) Set `android:stopWithTask="false"` for your `Service` in `AndroidManifest` 3) Override `onTaskRemoved` for your `Service` and call `this.stopSelf()` there. – Anatolii Jul 28 '18 at 20:05
  • 1
    @RajeshK if it's still not clear maybe you should create a separate question for this because it's not easy to demonstrate how to do it in a comment :) – Anatolii Jul 28 '18 at 20:08
  • Thanks Once again for answering.. You are helping me a lot. – Rajesh K Jul 29 '18 at 09:21
  • I have already created a question here it is https://stackoverflow.com/questions/51572385/activity-and-jobintentservice-lifecycle . Please have a look. – Rajesh K Jul 29 '18 at 09:23
1

No it won't if you BIND the service and manage the binding (through ServiceConnection) in the "OnResume" Lifecycle Method and then you UNBIND the service on the "OnPause" Lifecycle Method. ( Never tryed but I think this will work. Also if you override the return of the OnBind method in a JobIntentService you need to call the "onHandleWork" from the "onBind" by starting a new Thread").

( Tell me if you need some code examples and I will edit this answer when I have time (: )

Happy coding. Bye

Z3R0
  • 1,011
  • 10
  • 19
  • Thanks for the answer. But for now I have used the previous answer. Thanks once again. – Rajesh K May 18 '19 at 09:16
  • Np, it is best asnwer if your ui isn't visible. Mine answer if your activity is visible and need to update ui using service progress (: See you, have a nice coding – Z3R0 May 23 '19 at 13:20
  • Can you please answer one more question. What is the maximum time for which a background service can run for the three situations 1) If the app process is running the app is visible to the user 2) if the app process is running but the app is not visible to the user(maybe the user has minimized) 3) if the process is not running and the app also is not visible to the user. – Rajesh K May 28 '19 at 23:29
  • Hello Rajesh, for early version of Android (> .08) for experience I saw that you need to elevate to foreground priority every service that need to do important task and if it doesn't agree to specification u can found here: https://developer.android.com/about/versions/oreo/background In that case, if it don't agree these specifications, the service will be killed very fast so you need to put it in foreground. I don't know exactly how many time it will last, (1) theorically if the app is running and it is visible android system it won't kill the service immediatly – Z3R0 May 30 '19 at 08:35
  • he will kill your service after some minutes (or never, idk exactly) until the app goes in background or it is closed. (2) if the app is in background you need to follow the specifications in the link and the service will be killed really soon so you need to put it in foreground (3) idem as 2 you MUST put the service to foreground So if the app is not running or if the app is not in foreground you need to put the service to foreground. This is a security policy because the USER MUST KNOW THAT A SERVICE OF A SPECIFIC APP IS RUNNING. In older version of Android there aren't – Z3R0 May 30 '19 at 08:39
  • so many limitation on background services. I remember you that when you develop an application with service you should use JobService and JobIntentService and manage the start of the Job in 2 different ways based on the version of the device on which the app is runnign: on Android < 8.1 you can start the Job with "startService" or with "JobScheduler" or with "enqueueWork". "EnqueueWork" works for all the versions, but, if I remember right, inside the JobService if you are in Android version < 8.0 you need to ask, if you need it, for the WakeLock instead on version > 8.0 it manage it – Z3R0 May 30 '19 at 08:46
  • it manage it implicitly inside the call; infact "JobService" has a method "unqueueWork" which you can override and call to start the service. Also remember to right manage the periodic jobs and the latency jobs! Infact for Android Version < 8.0 (API 26) you can use the scheduling of periodic jobs and it will works right, bt on Version > 8.0 you can't so you need to manage the periodic jobs by re-scheduling it inside the JobService lifecycle (Personally I reschedule it inside the "onStartJob" method override of my JobService class) – Z3R0 May 30 '19 at 08:47
  • Last thing JobIntentService (and maybe JobService too, I don't remember now... u need to try it and please tell me after you tryed :D xD) works like IntentService and can't be started together because it are managed as a queue of works so if you start 10 times the same JobIntentService it will run the first and after the first it will start the second and after it the third and so on... Hope this is helpful for you! :D Actually I'm at work but if you need some code tell me and I can't edit my post and add some when I have time. See you and have a nice c0ding! – Z3R0 May 30 '19 at 08:51
  • (I also hope that I told you right things because I wrote you what I remember by study and coding experience without check it on the net... so maybe I remembered wrong something! So if you're not sure about anything I wrote check it on the net and if I wrote something wrong please write me about that! I'll really appreciate it because for me it is really important to remember the right things (: Thank you! ) – Z3R0 May 30 '19 at 08:55
  • Hi. Thanks for the answer. I have started a bounty for 50 points and will award that to you for your really helpful nature. I cannot award now but will award after 24 hrs(StackOverflow limitation). You have a really helpful nature. Thanks once again. – Rajesh K May 30 '19 at 16:43
  • Thank you, but wasn't necessary (: Tell me if you have any news about that, also if I wrote something wrong! It's really important for me! Thank you again, have a nice day and happy coding :D – Z3R0 Jun 06 '19 at 09:27
  • Hi, Just to update you whatever you have written is 100% right. And is working in my test device. Thanks – Rajesh K Jun 14 '19 at 11:42