Android SyncAdapter回调 [英] Android SyncAdapter Callback

查看:246
本文介绍了Android SyncAdapter回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在SDK中的SimpleSyncAdapter示例项目中实现了SyncAdapter,AccountManager和私有ContentProvider。

I have implemented a SyncAdapter, AccountManager and private ContentProvider along the lines of the SimpleSyncAdapter sample project in the SDK. It is all working well.

现在,当我从远程服务器下载了具有特定标志的新行时,我想向用户显示一条消息。当同步完成时,我需要来自SyncAdapter的回调,以便我可以执行查询并显示活动中的消息。我已经看到了一些关于StackOverflow讨论这个问题,但没有一个很好的答案。

Now I want to show a message to the user when new rows have been downloaded from the remote server that have a specific flag set. I need a callback from the SyncAdapter when a Sync has finished so I can do the query and display the message from an activity. I have seen a few questions on StackOverflow discussing this but none with a good answer.

如何从Android SyncAdapter监听进度?说SyncStatusObserver是无用的。用户mobibob建议使用ResultReceiver从同步线程回应UI。

How does one listen for progress from Android SyncAdapter? says that the SyncStatusObserver is useless. User mobibob suggests using a ResultReceiver to respond back to the UI from the sync thread.

如何知道同步何时完成?建议在SyncService中使用Intent。

How to know when sync is finished? suggests using an Intent in your SyncService.

< a href =http://stackoverflow.com/questions/5785482/how-to-signal-sync-complete-to-the-android-syncmanager>如何向Android SyncManager发送同步完成信息?建议使用SyncResult。由maxpower47链接的示例代码使用SyncResult类报告异常,但不实际报告同步是否已成功完成。

How to signal sync-complete to the Android SyncManager? suggests using the SyncResult. The example code linked to by maxpower47 uses the SyncResult class to report exceptions but not to actually report if a sync was successfully completed.

我只是不知道是哪个是

推荐答案

这是一个古老的问题,但是我没有看到任何解决方案。在过去的几天里做了一些研究,并没有很多的例子在syncAdapter处理网络请求和通知UI。

This is an old question but I did some research in the past days and there are not many exemples on syncAdapter handling network requests and notifying the UI.

首先你应该使用Loader with contentProvider使你的生活更轻松。你不需要注册内容解析器不再Loader为你做它。因此,这意味着您的UI会通知您进入内容提供商的任何内容。

First you should use Loaders with contentProvider to make your life easier. You don't need to register for content resolver anymore the Loader does it for you. So it means your UI gets notified for anything that goes into your content provider.

如果没有更改,该怎么办?一切都是最新的或您有网络错误。

What if nothing changed ? everything was up to date or you had a network error.


  1. 您可以将syncAdapter的状态作为Google I / O
    应用程式,在 BaseActivity

  2. 我看了默认的Android电子邮件应用程序,他们使用
  3. 您可以使用broadcastIntents或使用eventBus(例如方形Otto)通知您的UI任何行为。

我喜欢最后一个更好的,因为它给你更多的粒度在syncAdapter发生的事件。

I like the last one better because it gives you more granularity on the events that happen in the syncAdapter.

这篇关于Android SyncAdapter回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆