简体   繁体   English

android 4.0模式下载/同步数据

[英]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. 我有一个android 4.0的应用程序(android:minSdkVersion =“14”),有很多SQLite表,需要从我的服务器下载(不删除或发送)新数据。 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. 目前用户有一个同步按钮,但我想实现一些在3G / Wifi开启时每1-2小时自动同步的东西。

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) . 使用服务广播接收器(对于wifi状态,如上所述) Remember that polling a web-server every X times is a really bad experience in terms of battery usage. 请记住,在电池使用方面,每X次轮询一次Web服务器是非常糟糕的经历。 Try building a GCM web service. 尝试构建GCM Web服务。

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 我建议你读的Android的第19章临4( http://my.safaribooksonline.com/book/programming/android/9781430239307/chapter-19-broadcast-receivers-and-long-running-services/navpoint-164 )然后使用已经准备好的源代码来自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) . 我通过使用Alarm + ALongRunningReceiver(=广播接收器) + ALongRunningNonStickyBroadcastService(本地服务) + LightedGreenRoom(处理CPU partial_wake使其不会入睡)来完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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