0

I am trying to start a timer at regular interval (through handler) and and write gps location to file upon expiry. However when my app is in the background for a while it seems that it is getting killed or not writing anymore.

Why? Is there a way to do this?

Thank k you

Snake
  • 14,228
  • 27
  • 117
  • 250

2 Answers2

1

If you want your app to run in the background, you need a Service.

Alternatively, if you only need to do small tasks every once in a while, you can use a BroadcastReceiver.

hatcyl
  • 2,190
  • 2
  • 21
  • 24
  • I find service to be a lot of work (given what I have now)or heavy and I am trying to avoid it (as much as I can). Would you using Alaram manager/broadcast receiver to open a file/write GPS location every second be too much? – Snake Jul 18 '13 at 05:02
  • For every second, I think I would personally stick to a Service. And, as you where initially using, Android docs recommend to use a Handler in your case. (But you need to use the Handler in a Service not an Activity.) – hatcyl Jul 18 '13 at 05:44
  • Thank you so much. this maybe off topic, but how can I update Activity Textfield values from the service? – Snake Jul 18 '13 at 05:47
  • Great answer here: http://stackoverflow.com/questions/2468874/how-can-i-update-information-in-an-android-activity-from-a-background-service – hatcyl Jul 18 '13 at 05:53
0

A Handler is stopped indeed when the process is completely hidden, and nothing else is going on in the process. You could use a Service instead, that is "sticky".

  • Thank you , I was hoping there is an alternative solution to service. Check my comment to the other answer below – Snake Jul 18 '13 at 05:02