views:

47

answers:

1

In my application I implemented a remote service that exchange data with a webserver each 20 minutes and modify the value of some object in MyApplication Class that extends Application.

After 12 or 15 hour android kill MyApplication class and/or my Service. I tried to implement the

android:alwaysRetainTaskState="true"

without results. Some could explain to me how to make a service or an application persistent in android? I see more application that stay in background for days without problems, but I don't know how to do that.

I know that android could kill each application if it needs some free memory but my system has 180mb of free memory and no application running because I use it for testing my programs.

+1  A: 

Some could explain to me how to make a service or an application persistent in android?

You don't. You write your app such that it does not need to be in memory all of the time.

In my application I implemented a remote service that exchange data with a webserver each 20 minutes and modify the value of some object in MyApplication Class that extends Application.

Please use AlarmManager and an IntentService, so your service does not need to be in memory except when it is doing meaningful work. This also means Android is rather unlikely to kill your service while you are in memory, and users are unlikely to kill your service because they think you are wasting memory.

CommonsWare
thansk for reply commonsware. so I have to extend IntentService and not Service? I will use AlarmManager instead of a TimerTask.is it right? another question: when I use alarm manager the service are started each 20 minutes?
zerocool87
sorry if I post another question but I can't edit the previous post. If the service are not ever in memory is it the same for the application class.so i have to save my object and variable in a text files or as Shared preferences or not? Thanks again
zerocool87
@zerocool87: "so I have to extend IntentService and not Service?" -- that's typical with `AlarmManager`. "I will use AlarmManager instead of a TimerTask.is it right?" -- yes. "when I use alarm manager the service are started each 20 minutes?" -- I would let the user choose the frequency, but otherwise it can be whatever you want. ".so i have to save my object and variable in a text files or as Shared preferences or not?" -- yes, your state needs to be in a text file, shared preference, database, or something.
CommonsWare
I used 20 minutes as an example :) So I have to implements service as IntentService and use alarmManger to wakeUp the service each 20 or 30 minutes. In this way can I wakeUp the service from the activity ? I don't understand if, with alarm manager, the service are stopped and restarted or simply is "sleeping". Thanks for you reply
zerocool87
@zerocool87: "In this way can I wakeUp the service from the activity ?" -- there is no activity. "I don't understand if, with alarm manager, the service are stopped and restarted or simply is "sleeping"." -- the service is stopped. `IntentService` operates on the command pattern, taking in an Intent from `startService()`, passing it to a background thread and `onHandleIntent()`, and then shutting down automatically when the work is complete.
CommonsWare
Thanks for your explanation. I will modify my Application in this way.
zerocool87