I'm using a service in android which every 3 seconds will be downloading a JSON. My problem is that this service consumes me much battery and mobile data plan. How to download information to only if there really is a new data or update data?
-
I'd say reduce the interval of downloads to something more reasonable, like a half-hour to 2 hours...why does it feel the need to download JSON every three seconds? – Makoto Apr 30 '14 at 05:15
-
Thanks,Because, I need make some similar to a realtime application, for all user can see data immediately when any user make a change, some equals a chat room – user3236034 Apr 30 '14 at 05:31
2 Answers
Yes, that's pretty battery draining. And it will be even more if your JSON
is big. I'd definitely use some mechanism here that would send me a "signal" whenever this JSON
has changed. I presume that this is stored somewhere in a remote server, so you could use Google Cloud Messaging
here: Whenever it changes, your remote server could send the device a message telling them there's new data available to be downloaded and download just at that time.
This way you're not the one who's asking, but you're being informed when there's something new, so you won't be draining the battery.
I guess this might help you if you want to deploy GCM
:
If data is located on your server, you can use GCM to notify clients, that the new data is available. Once you receive such notification, pull the new data to the device. If data is not on the your server, you should just pull data periodically. How it can be done? Use AlarmManager
to set up alarm, which will start downloading Service
. Please note, that you should use setInexactRepeating
, it will save battery better. And the last note is that it's better to use HttpUrlConnection
instead of Apache HTTP Client
for a devices with Android version >= Gingerbread.

- 11,166
- 2
- 35
- 45