4

In My app I am trying to download images even though user has force stopped it and exit it using recent app manager by swiping . Why I want this behavior, it has only one reason and that is :

Because I want User to stop the download from my button given in activity

So For this I have made async task at first place , it was working nicely even the activity is destroyed, But When I destroy/kill my app using recent app manger the app gets killed and so the service

Then I moved my async task to the Service which is Sticky , I thought it should not get destroy when the user closes the app from recent app lists, but The service stops. though some times it start again , but due to some reason I just want it to not to stop and download like a google play store does. What ever you do , you can not stop downloading of app , until and unless you use stop button in the google play store app.

Then I read about IntentService, about intent service I read that ,

You can run intent service and forget about it , as it stops itself after completion of task.

So I though this is something , which could not be killed even user stops it or close app from recent app list by swiping , as intent service would perform its task and would stop itself

But All in vein. I have read many things about the service and how to cure service from being not killed. I came to know about starting service as forground.

But what other ways do other Top class apps adopt to keep the background working all the time , like security apps , gps based apps, and antivirus apps whose services run all the time on background. You can examine same behaviour inn google play store app , you can start downloading of any app and exit the play store app , even from recent app tray , but it would continue to download until and unless you go to notification bar click notification , get navigated to place where you finally see stop downloading button to stop downloading the app.

So How can I achieve this ? what is wrong with service or what else can I use to get the same behavior as google play store has.

Please guide me . Your comments and discussion would be appreciated.

Coas Mckey
  • 701
  • 1
  • 13
  • 39

3 Answers3

1

It sounds like you want to run your service in a separate process from your main application so it does not get killed if the app is removed from the "Recent Tasks" list.

This is made possible by using the "Process" attribute explained in the documentation here: http://developer.android.com/guide/topics/manifest/service-element.html#proc

Kuffs
  • 35,581
  • 10
  • 79
  • 92
0

What you need is a ForegroundService. The android developer guide about services states that:

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Eric B.
  • 4,622
  • 2
  • 18
  • 33
  • I know about this , but I wanted to know any other way ? and how is that google play store app working – Coas Mckey Nov 11 '15 at 13:00
  • Look, any notification you cannot dismiss from the notification bar is a ForegroundService. Which means google play or any other non dismiss-able notifications are Foreground Services. – Eric B. Nov 11 '15 at 13:01
  • Not true. A notification can simply have its FLAG_ONGOING_EVENT flag set which would prevent it being dismissed. It does not necessarily have a service attached to it. – Kuffs Nov 11 '15 at 13:05
  • how to start the intent service as a foreground service what command do I need to write – Coas Mckey Nov 11 '15 at 13:34
  • Use the search box at the top of the page.: http://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service – Kuffs Nov 11 '15 at 13:39
  • @Kuffs I didn't know that. I guess that's something i will have to try. – Eric B. Nov 11 '15 at 16:17
0

You can use return return START_STICKY in onStartCommand in service

Syed Danish Haider
  • 1,334
  • 11
  • 15