简体   繁体   中英

android 4.0 pattern for download/sync data

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?

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.

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.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM