Android的SyncAdapter回调

Android的SyncAdapter回调

问题描述:

我实现了一个SyncAdapter,的AccountManager和私人的ContentProvider沿着SDK中SimpleSyncAdapter示例项目的线条。这是一切运作良好。

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回调时,同步完成,所以我可以做的查询,并从活动显示的消息。我看到的*的几个问题有一个很好的答案讨论这个,但没有。

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 * discussing this but none with a good answer.

How没有人会聆听来自Android的SyncAdapter进展如何?说,SyncStatusObserver是没用的。用户mobibob建议使用ResultReceiver来响应从同步线程的用户界面。

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。

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

How信号同步完成到Android SyncManager?使用SyncResult建议。这个例子code。通过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.

我只是不知道这是最好的选择,我还没有看到任何的地方这些解决方案所使用的所有示例项目。

I just don't know which is the best option and I have not seen any example projects where any of these solutions are used.

我知道这是一个老问题,但我问了同样的事情自己。 我发现了一个很好的解决方案,特别是因为我负责的,你是本地数据,方法是使用ContentResolver的方法如下:

I know this is an old question, but I was asking the same thing myself. What I found out as a good solution, specially because I'm dealing with local data as you are, is to use the following method from ContentResolver:

registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer)

该寄存器得到回调时确定一个给定的内容URI的数据发生变化观察者类。但是,这只是如果你的ContentProvider发送通知。因此,举例来说,如果你想在ContentObserver以上通过ContentProvider的做你的数据库中的所有更新,您的ContentProvider应实施更新与此类似得到通知:

This register an observer class that get callback when data identified by a given content URI changes. But that can only happens if your ContentProvider send the notification. So for example, if you want to get notified on the ContentObserver above for all updates done on your database through a ContentProvider, your ContentProvider should implement update similar to this:

@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
    // code goes here
    this.getContext().getContentResolver().notifyChange(uri, null);
    return 0;
}

使用notifyForDescendents当你做一个registerContentObserver是非常有用的。

Using notifyForDescendents when you do a registerContentObserver can be very useful.