Upload large file in background (service restarting when the app closed)
Asked Answered
P

2

7

I would like to upload large files (~10 - 100Mb wifi or mobile network), but in background, because the user maybe will leave the app and later the system will close the app (if not enoguh memory) I created a service for this case but my problem is that when i killed the app the service restarting and the uploading start again. I found same problems without solution:

keeping background service alive after user exit app

My service is restarted each time the application is closed

So it won't work, but what is the solution? How does the youtube app???

Pyrosis answered 2/1, 2014 at 20:55 Comment(1)
You can't enforce to keep running but you can implement a way to resume uploads after your app got killed.Falconer
F
2

You should use a foreground service via the startForeground() method if you are concerned about the possibility of the service being killed.

From the Service Lifecycle Docs:

  • A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)
Franglais answered 2/1, 2014 at 21:28 Comment(1)
This will handle most normal cases, but also remember other cases where a service might go away, like if the device shuts off. It would be nice if your app could recover from that as well.Clarion
C
-1

Do you have control over the server? This seems just like:

Uploading big files over HTTP

Googling brought up a proposal for the Gears project:

https://code.google.com/p/gears/wiki/ResumableHttpRequestsProposal

If you can use a server/plugin/module that will allow for Ranged PUTs, that's your best bet, otherwise you may have to roll-your-own "chunking"... Depending on your tools and knowledge, that may be the best option anyway, you could tweak it to optimize it for your specific mobile conditions.

Clarion answered 2/1, 2014 at 21:14 Comment(1)
The problem here is how to handle this problem on android side.. say you want to upload a very large file to S3..Scaremonger

© 2022 - 2024 — McMap. All rights reserved.