如何在没有任何用户请求的情况下更新数据库?
我正在尝试开发一个应用程序,它将从服务器下载一些 json 字符串(问候语)。由于我是 android 新手,我不知道具体如何做到这一点。该应用程序将有自己的收件箱,它将通过 sqlite 实现系统会自动从服务器下载所有最新的问候语(json 字符串)并将它们放入收件箱中。那么最好的方法是什么,我的意思是我应该使用服务还是异步任务???我希望每当新的问候来到了服务器,我的应用程序应该在没有任何用户请求的情况下自动下载它,并且当用户打开收件箱时,它是最新的。
I am trying to develop an application which will download some json strings(greetings) from server .As I am new to android,I don't know exactly how to do that.The application will have its own inbox which will be implemented through sqlite and system will download all the latest greetings(json strings) automatically from server and will put them in the inbox.So what is best approach for this,I mean should I use SERVICES or Async Tasks for it ???I want that whenever a new greeting comes to the server, my application should download it automatically without any user request and when user opens the inbox,it is uptodate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务更适合您在此处描述的用例。服务旨在用于以下场景:您不确定数据何时到达,并且希望在后台继续检查数据而不让用户知道正在发生某些事情,另一方面,Asynctask 在以下情况下更有用:你需要做一些依赖于网络的事情,或者在用户点击某件事后需要一些时间的事情。异步任务只是在应用程序处理用户请求时向用户显示适当的等待进度条或其他内容的另一种方式,以便用户不觉得应用程序被卡住了。所以服务适合您的情况。
Services are more appropriate to the use case that you have described here. services are meant to be used in the scenarios when you are not sure when the data will be arriving and you want to keep checking for it in the background without letting the user know that something is going on Asynctask on the other hand is more useful when u need to do some network dependent thing or something that will take time after a user clicks on something.The asynctask is just another way to show the user a proper waiting progress bar or something while the application is processing the user's request so that the user doesnt feel like the app is stuck. So SERVICES it is for your case.
如果您想更新数据库,即使应用程序未运行,您将需要服务或
服务 同步帐户
http://marakana.com/forums/android/examples/60.html
对于同步适配器
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html
我使用服务实现了类似的东西
if you want to update the database even if the app is not running you will need a service or a sync account
for services
http://marakana.com/forums/android/examples/60.html
for the sync adapter
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html
I implemented something similar using a service