0

I have some data in my SQLite database table. When my app starts, I want to send that data to the server every 5 minutes.

When the app is closed, it should stop.

  • What is the best approach for this?
  • Should I use Service or IntentService?
  • Should I use AlarmManager, Handler or any other thing?
  • I'm aware of my application speed. I don't want to make it slow. What is the effective approach?
Harshitha Palihawadana
  • 2,171
  • 2
  • 15
  • 20

3 Answers3

0

If you are only transmitting when the app is in foreground, you can do it with a Handler.
You start the handler in onResume() and cancel it in onPause(). And perform the transmision with an AsyncTask or in a separate thread.

If you need to stransmit in background, you can use a service instead and schedule it with the AlarmManager.
And then start the service from the app's Activity.

I am not sure how IntentService would be used for this.

Juan
  • 5,525
  • 2
  • 15
  • 26
0

As you just want to run your process while the app is foreground, then TimerTask or Handler with conjunction to Message or Runable is good. It won't bother much your app's performance. If you ask about the better one from these two I'll say it's Handler. Check the details here in this answer:

https://stackoverflow.com/a/3975337/4128371

But if you want a really good performance then I'll suggest to go with AlarmManager. Otherwise Handler is a good option.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
0

Alarm manager is not precise, an alarm schedule for 5 minutes may be that when the device is sleeping it will be fired at twice or triple the time.

If you want accuracy the device does not have to fall in sleep. ( I know that will shorten battery duration)

If you want to prevent the device to go to sleep you need to launch a foreground Service with a non dismissable Notification. That's the Service has to call startForeground()

While the device is awake both Alarmamanager and Handler + Runnable will be accurate ... I prefer the Handler.

from56
  • 3,976
  • 2
  • 13
  • 23