3

I have an application for android 4.0(android:minSdkVersion="14") with lots of SQLite tables that needs to download(not delete or send) new data from my server. At the moment the user has a sync button, however i would like to implement something that would synchronize automatically when 3G/Wifi is on and every 1-2 hour.

What pattern or best practice should i use? an Alarm thing, or a local service, or a remote service, or something else?

max4ever
  • 11,909
  • 13
  • 77
  • 115

3 Answers3

0

You need two things:

Make sure your code runs in his own thread, you don't want to execute anything heavy in the broadcast receivers. A good place to run such a code would be a service with his own separate Thread.

Community
  • 1
  • 1
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
0

Use a service and a broadcast receiver (for the wifi state, as mentioned above). Remember that polling a web-server every X times is a really bad experience in terms of battery usage. Try building a GCM web service.

Community
  • 1
  • 1
0

I would recommend you read Chapter 19 of Android Pro 4( http://my.safaribooksonline.com/book/programming/android/9781430239307/chapter-19-broadcast-receivers-and-long-running-services/navpoint-164 ) and then use the already ready source code from here http://www.androidbook.com/akc/filestorage/satya/documentfiles/3810/ProAndroid4_Ch19_TestReceivers.zip

I did this by using Alarm + ALongRunningReceiver(=Broadcast Receiver) + ALongRunningNonStickyBroadcastService(Local Service) + LightedGreenRoom(Handles CPU partial_wake so it doesn't fall asleep).

max4ever
  • 11,909
  • 13
  • 77
  • 115