0

I have the following problem:

My android application requires some setup, which can take several minutes. It is executed only once at the time of first launch. So, when a new user starts the application for the first time, a special initializing process starts and a progress dialog with caption "Preparation for the first launch" appears. When this step is complete, it isn't needed next time.

The problem is, that while this process is being executed, the user can switch to another activity - to start another application or make a call. And it entails the death of this setup process. What should I do to avoid this?

Take into account, that this setup process is run in IntentService. However it doesn't help.

Thank you in advance.

akor81
  • 11
  • 1
  • 3
    There is no way to prevent "Android system from taking resources/memory from your app and allocating to current app in foreground if System thinks it has less memory available for it " – Sharp Edge Aug 31 '15 at 13:27
  • you need to implement broadcastreceiver refer http://developer.android.com/reference/android/content/BroadcastReceiver.html – arun Aug 31 '15 at 13:30
  • if the process is getting killed I would save the state of the information you received along the way and just check how much infor you have vs how much is required and then resume from the point of the last bit on info you saved – tyczj Aug 31 '15 at 13:33

2 Answers2

0

You can't prevent a Service from getting killed. You can however start it with the flag START_STICKY. Then it will be restarted as soon as there are free capacities again.

You'd need to save it's state somehow though. You can use e.g. onDestroy for that.

You could also look here for some tricks how to make it more likely that your service survives.

Community
  • 1
  • 1
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • Thank you! Your reference was very helpful. The method startForeground (and then, at the end, stopForeground) solves my problem. – akor81 Sep 01 '15 at 06:21
0

Thank you, guys! With help of @F43nd1r I found the satisfactory solution: at the start of my process I call startForeground() and it prevents the service from death. Then, at the end, I call stopForeground().

akor81
  • 11
  • 1