prevent app to clear application data while running in background - android

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.

Related

Android Activity has destroy after few minutes when app is pause

I create an application which get location and send data to server in 5 min intervals. But when my app is paused after 2/3 min app is destroyed. I want to running service till I destroy activity. Please give some suggestion.
There are so many reasons lets your app crashes when going background. You should give some code.
Your problem, I think your device is Android O or later. In Android O, if you create a BACKGROUND service, the app will be destroyed in the background, you should use FOREGROUND service instead.
Reference: https://developer.android.com/about/versions/oreo/background
If you intend your application to work even when the app is not visible to the user, use a Service, which runs in the background.
Using a basic Service is just as easy as creating an activity, give it a try.
Also, posting relevant code that shows what you've tried could also help.
Since this app runs in the background, be sure to optimize usage of location services so as to save the phone's battery.

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.

Phonegap app on Android platform - keep app running

I have a Phonegap app that functions as a communication service for a specific group of people. Using the local notification plugin found on the phonegap-plugins GitHub page, I have implemented notifications into the app, so that whenever someone receives a new message, a notification will appear if the app is in the background.
After about an hour, though, no more notifications will occur, and it appears the process was killed. When I go back into the app, it starts completely over instead of resuming from where I last left off. I'm assuming that after a certain amount of time, Android stopped running the app in the background.
Does anyone know how to keep the app running in the background until it is told by the user to stop, and prevent Android from killing the process?
As CommonsWare suggests, you could write a dummy service to keep your app alive, but also as he rightly suggests, if you're going to go to the effort of writing a native dummy service, you may as well write the actual service natively and have done with it.
As a bit of a quick and dirty solution, you could maybe use a partial wakelock (see here) to keep the CPU running your app while it's in the background.
I successfully used this approach to keep my Phonegap-based walk navigation apps alive in the background so they can continue to receive and process position updates.
In your case, staying alive to receive notifications isn't exactly what a partial wakelock was intended for and so I'm unsure whether android will kill your app anyway after a while since it's not doing anything (unlike mine which is constantly receiving and processing position updates) but it might do the job without needing to write a service, so may be worth a try.
Have a look at my answer to this question which contains my code for an updated version of the PowerManagement plugin for Android. I updated the plugin for use with Cordova 2.8.0 but also extended it to be able to acquire a partial wakelock.

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.

Android retain AsyncTask state/progress

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?

Categories

Resources