Android retain AsyncTask state/progress - android

In the app I'm developing atm. I use asynctasks to upload videos to a website, as it stands now if the application process is killed (User returning to home screen using the back key), those asynctasks are lost. ideally I would want the uploads to carry on despite the application process being killed, but I don't think that is possible.
I wonder if there is a way to retain their progress somehow (Maybe support from the website API is necessary?), or if not at least save the details of the asynctask and restart it when the app is opened again.
Vimeos application seems to have been able to resume video uploads, even after having killed the application process, thats exactly what I'm hoping to achieve.
Appreciate any ideas and suggestions.

I think you may be using the wrong architecture.
Anything that needs to survive in between Activity transitions is more suited for a Service. A service runs in the background (possibly even after the app is closed) and lets you do long running things such as performing uploads.
To kill the app process but have the Service continue to run, you can assign the service to a separate Android process using android:process in the manifest.
http://developer.android.com/guide/topics/manifest/service-element.html#proc
See this thread too:
How to keep a service running in background even after user quits the app?

Related

Android Saving Images(with some processing) Optimal Method

I have obtained images from the camera and want to save it to a directory after some processing which takes about 10 seconds. I tried the following methods :
1) Asynctask(Problem : if the user closes(swipes) the app, the asyncTask is killed)
2) Intent Service(Problem : same as Asynctask)
3) Foreground Service(Problem : Hangs the UI of the application)
4) Running on UI Thread(Problem : Hangs the application).
So my question is what should I use to save my images(with some processing) such that all tasks are done even if app is closed and the UI does not hang(freeze).
Any help would be appreciated.
You cannot prevent Android from killing your process. If the user wants it killed (for example, using "force stop"), it will get killed. There is nothing you can do to prevent that.
When the user swipes the task from the recent tasks list, the behaviour is different in different versions of Android (and also different manufacturers have different behavour). However, in most cases, the OS Process hosting your app (including any services) is simply killed. If the app had a running Service that wants to be restarted, Android will create a new OS Process and reinstantiate the Service and restart it.
You can mitigate this a bit by putting your Service in a separate OS process. To do this, add android:process=":remote" to the <service> declaration. On some versions of Android, swiping the task from the recent tasks list will then kill the OS process hosting your activities, but NOT kill the OS process hosting your Service.
In any case you need to make your app robust so that it can handle being killed and restarted.
Thanks everyone for suggestions. How I finally was able to achieve this was using a foreground service which itself created and asynctask to perform the work in the background. I am a novice so don't exactly know how it's working, but it doesn't cause any app freezing/lag and does the work even if the app is swiped from the recent tasks list. (tested in Android L(5.1.1) and M)

prevent app to clear application data while running in background

I am developing an android application related to gps. that is sending location information periodically to the server. sometime i am getting process killing issue in some devices.
if my application running in foreground then its working file. but while we minimize app and move it to back or once phone going to sleep then app automatically clearing all application data and variables. and then when we tried to resume activity back, it showing blank information and generating exception.
how to prevent clear app data while app running in backgtound? i want to keep this information as it is. this issue only arise in some devices not all.
i have also tried research on Google, but not getting any good solution.
Please Help me. Thank you.
You can't do it like that. If you want your application to keep sending data to a remote service even if the app is in the background, you need to do it a long running Service. Keep in mind that a service runs by default in the main thread, so you need to run your comms with the server in a background thread (however you like it, asynctask, wtv). Then, to really make sure Android doesn't kill your service if it's running low on memory, you need to set your service as a foreground service with a notification.
That way, even if your app is sent to the background you're sure Android the communication will continue.

Android:nonstop Back ground service

I have started a service from my application and from that service a worker thread is started .I want my service to run even application goes background and until the user kills/exits the application.
But some cases my service got killed due to low memory ,then used sticky service or making the app to foreground to restart the service.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method, but in this case how we can control that thread.
Please let me know is it the right approach ,and is this usecase achievable
I want my service to run even application goes background and until the user kills/exits the application.
This is not possible. The user can always get rid of your app, via Force Close in Settings, or via some device's version of the recent-tasks list.
But some cases my service got killed due to low memory
No, your process is terminated for low memory.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method
No, because your process is being terminated.
Please let me know is it the right approach
Probably not. Very few apps need a service that runs constantly, which is why Android, and its users, go to great lengths to control such services. I would recommend that you try to find some solution to whatever your problem is that does not need a service running constantly.

Continue uploading even when the app gets killed(Android)

I want an upload operation to get completed even when the application is running in background. The process should get completed even if the application gets killed while opening other application. Could anyone pls suggest me some solutions?
Use a service, and attach an ongoing notification to it. This will prevent Android from killing your app by and large, but it is still possible you may be killed in the most extreme scenarios. However, this is the best you can do, unless your app is in the foreground throughout the upload.
Your application do not get killed by opening other applications. Check the Android life cycle. And the best way to upload a file is to do it on a thread. Whether you close the app or not started thread will keep working on.

Keep android application running while in background

I have created an application in Android. This application should never die (even when sent to the background).
Currently, after a while the Service manager returns "No longer want..." and terminates it.
I've read that one solution whould be to create a service. However my application is way to complicated to be splitted into two functionality sets (one for the service and one for the application).
Is there any "trick" in order to keep my application running at all time?
Could I create a dummy service inside my application that might force the android to keep my application alive?
Is there any other way?
FYI: 1) It's a customized application that will not be released on the Market. 2) Handsets can't be rootted.
Thanks
You must create a Service to have a persistently running app, even after all your Activities have been sent to the background due to user pressing the Back button, answering a call, switching to another app, etc. You should review the Android Process Lifecycle which states that:
Sometimes an Activity may need to do a long-running operation that
exists independently of the activity lifecycle itself. An example may
be a camera application that allows you to upload a picture to a web
site. The upload may take a long time, and the application should
allow the user to leave the application will it is executing. To
accomplish this, your Activity should start a Service in which the
upload takes place. This allows the system to properly prioritize your
process (considering it to be more important than other non-visible
applications) for the duration of the upload, independent of whether
the original activity is paused, stopped, or finished.

Categories

Resources