Frage

My android app has a ContentProvider and stores data (strings) in a SQLite table These rows need to eb sent up to an internet server

I want this data to be uploaded in the background when the remote host is available (WiFi on for instance)

The data should be sent asap (when network available of course) without interfering with my app's ui It should send even when my app does not have the main focus (user may be sms-ing or using the camera)

New data could be added to the table (out queue) every 5 seconds

Q: Should I use a background service to send the unsent rows?

It seems too frequent to use the "Alarm Service", but I could be wrong

Battery life is my main concern, but secondly is that it needs to send as soon as possible (or shortly after internet available)

Android Studio targeting API 17+

War es hilfreich?

Lösung

A background service would be best to handle the request and save battery life. You could either check the connection state and send yourself and just let the service handle when there is no connection, OR better yet just use the service to always send the data and let it decide when to send it.

As for the service, see this question: How to be notified on wifi network status change?

The idea would be to use the code in that answer to implement a broadcast receiver in your service to detect when there is an available connection. When that connection is available then you could send the data.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top