11

I have intent service in my app. This service has some job to do - uploading some files. It runs only when there is something to do.

Service won't upload when some conditions are met, for example no Internet connection. For that reason it registers itself to on broadcast receiver to receive message about Internet connection changes.

The problem is that service is killed with app even if it is doing something, for example:

  1. App is sending intent to service
  2. Service started uploading something, everything fine
  3. X% uploaded, app is killed, service is killed
  4. Internet connection changed - service is woken up.

If service is woken up after app is killed, why is it killed with the app? Is there any way to prevent killing service when app is killed?

I'm killing app manually. I know android could kill my service anytime and I don't want to prevent it. I just want to have this service running after user closed or killed app.

Ari
  • 3,101
  • 2
  • 27
  • 49
  • What do you mean with 'killing' your app? – Stefan de Bruijn Oct 24 '13 at 10:10
  • 2
    @StefandeBruijn It is hard to tell. What I did was using home screen to show running app and then moved out my app. I want to keep my service alive if it is possible. I think it was possible, because android woke up my service on broadcast receiver. – Ari Oct 24 '13 at 10:22
  • did you find the answer of your this problem @Ari – Coas Mckey Nov 11 '15 at 09:35
  • Do you mean you want to prevent the service from being restarted or you want the service to continue to run even when app is killed? – Amr El Aswar May 25 '16 at 20:20

3 Answers3

1

Because you used an intentService that mean the intentService will destroy once the activty destroy so you have to use Service instead of intentService, so you can uplaod your file in the backgroud.

According to manipuation between the Service and the activty via broadcast receiver or to bind the service to activty.

Edit :

The Service may be triggered from any thread.

The IntentService must be triggered from Main Thread.

Basbous
  • 3,927
  • 4
  • 34
  • 62
  • intent service is better than service for me and it is not a problem that it is intent service – Ari Oct 24 '13 at 10:30
  • but it is getting back alive on broadcast receiver event – Ari Oct 24 '13 at 10:38
  • try to request a wakeup permission PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag"); mWakeLock.acquire(); – Basbous Oct 24 '13 at 10:45
  • "Because you used an intentService that mean the intentService will destroy once the activty destroy" --> this is not correct. IntentService will self-stop when it's done with all the requests. – Binh Tran Oct 24 '13 at 14:39
  • @BinhTran Who [among of you](http://stackoverflow.com/questions/12878150/how-to-keep-an-intent-service-running#answer-12881417) are telling the truth – mr5 Aug 06 '14 at 08:05
  • @mr5 Binh is telling the truth, and also the answer you link. The IntentService finishes when it finishes its execution, meaning it doesn't have to be stopped explicitly. On that answer, it says that is not meant to be running continuously. – mdelolmo Nov 13 '14 at 12:47
1

"It runs only when there is something to do." only theoretically :) - maybe that is you what you want to achieve.

"The problem is that service is killed with app even if it is doing something, for example:" Of course, there will be cases when the user action will end your Service or Intent service. This is a fail answer.

"Is there any way to prevent killing service when app is killed?" It is just watch for "parental control" task protection" keywords in Google!

Community
  • 1
  • 1
0

If you don't mind showing notification (in your case, you can for example show notification with upload progress), then in your IntentService (or Service) you can call:

startForeground(int id, Notification notification)

This should prevent killing your service when your application is killed.

From documentation: "You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing."

Konos
  • 1
  • 2