gpt4 book ai didi

android - cursor.setNotificationUri() 是做什么用的?

转载 作者:IT老高 更新时间:2023-10-28 22:20:48 27 4
gpt4 key购买 nike

我从 this tutorial 研究了如何使用 ContentProviders 和 Loaders

我是怎么看的:我们有一个带有 ListViewSimpleCursorAdapterCursorLoaderActivity。我们还实现了 ContentProvider

Activity 中,我们可以通过单击按钮调用 getContentResolver().insert(URI, contentValues);

在我们的ContentProvider实现中,在insert()方法的最后,我们调用getContentResolver().notifyChange(URI, null); code> 和我们的 CursorLoader 将收到它应该重新加载数据并更新 UI 的消息。此外,如果我们在 SimpleCursorAdapter 中使用 FLAG_REGISTER_CONTENT_OBSERVER,它也会收到消息,并调用其方法 onContentChanged()

所以如果我们插入、更新或删除数据,我们的 ListView 将会更新。

Activity.startManagingCursor(cursor); 已弃用,cursor.requery() 已弃用,所以我看不到 cursor.setNotificationUri( ).

我查看了 setNotificationUri() 方法的源代码,发现它在方法内部调用了 mContentResolver.registerContentObserver(mNotifyUri, true, mSelfObserver)CursorLoader 也是如此。最后 cursor 将收到消息,并在 Cursor 内部调用以下方法:

protected void onChange(boolean selfChange) {
synchronized (mSelfObserverLock) {
mContentObservable.dispatchChange(selfChange, null);
// ...
}
}

但我无法理解这一点。

所以我的问题是:我们为什么要在 ContentProvider 实现的 query() 方法中调用 cursor.setNotificationUri() ?

最佳答案

如果您调用 Cursor.setNotificationUri(),Cursor 将知道它是为什么而创建的 ContentProvider Uri

CursorLoader 使用 ContextContentResolver 注册自己的 ForceLoadContentObserver(扩展 ContentObserver) 为您在调用 setNotificationUri 时指定的 URI。

所以一旦 ContentResolver 知道 URI 的内容已被 更改 [这发生在你调用 getContext().getContentResolver().notifyChange(uri, contentObserver );ContentProviderinsert()update()delete() 方法中] 它会通知所有的观察者,包括 CursorLoader 的 ForceLoadContentObserver

ForceLoadContentObserver 然后将 Loader 的 mContentChanged 标记为 true

关于android - cursor.setNotificationUri() 是做什么用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com