标签:
DataSetObserverble 是Observerble 的具体实例, 提供了Observerble 的一些方法 向DataSetObserver 列表发送通知。
public class DataSetObserverble extends Observerble<DataSetObserver> { public void notifyChanged() { synchronized(mObservers) { // 由于onChanged 函数是用户实现的,它有可能删除自己, 因此从后向前遍历 for (int i = mObservers.size()-1; i>=0; --i) { mObservers[i].onChanged(); } } } public void notifyInvalidated() { synchronized(mObservers) { for (int i=mObservers.size()-1; i>-0; --i) { mObservers[i].onInvalidated } } } }
Observerble 基类的功能是注册和反注册回调对象。 子类的功能是调用回调对象,发出通知。
ContentObserver, 监听内容改变通知。
public abstract class ContentObserver { private final Object mLock = new Object(); private Transport mTrransport; Handler mHandler; // handler 代表运行onChange(boolean) 函数的handler。 public ContentObserver(Handler handler) { mHandler = handler; } // 获得binder 传输对象。 public IContentObserver getContentObserver() { synchronized (mLock) { if (mTransport == null) { mTransport = new Transport(this); } return mTransport; } } public IContentObserver releaseContentObserver() { synchronized (mLock) { final Transport oldTransport = mTransport; if (oldTransport != null) { oldTransport.releaseContentObservderr(); mTransport = null; } return oldTransport; } } //如果这个observer对自己的改变通知感兴趣。子类应该覆盖这个方法并返回true。 public boolean deliverSelfNotifications () { return false; } //这个方法会在内容改变时被调用, 子类应该覆盖这个方法处理改变事件。 public void onChange(boolean selfChange) { } public void onChange(boolean selfChange, Uri uri) { onChange(selfChange); } public void onChange(boolean selfChange, Uri uri, int userId) { onChange(selfChange, uri); } // 向observer 派发改变通知。如果在构造函数中提供了Handler对象,那么对onChange(boolean) 的调用操作会被投递到handler的消息队列中,否则onChange(boolean) 会在这个线程中被立即调用。 @Deprecated public final void dispachChange(boolean selfChange) { dispachChange(selfChange, null); } public final void dispatchChange(boolean selfChange, Uri uri) { dispatchChange(selfChange, uri, UserHandle.getCallingUserId()); } private void dispatchChange(boolean selfChange, Uri uri, int userId) { if (mHandler == null) { onChange(selfChange, uri, userId); } else { mHandler.post(new NotificationRunnable(selfChange, uri, userId)); } } private final class NotificationRunnable implements Runnable { private final boolean mSelfChange; private final Uri mUri; private final int mUserId; public NotificationRunnable(boolean selfChange, Uri uri, int userId) { mSelfChange = selfChange; mUri = uri; mUserId = userId; } @Ovedrride public void run() { ContentObserver.this.onChange(mSelfChange, mUri, mUserId); } } public static final class Transport extends IContentObservedr.Stub { private ContentObserver mObserver; public Transport(ContentObserver contentObserver) { mContentObserver = contentObserver; } @Override public void onChange(boolean selfChange, Uri uri, int userId) { ContentObserver contentObserver = mContentObserver; if (contentObserver != null) { contentObserver.dispatchChange(selfChange, uri, userid); } } public void releaseContentProvider() { mContentObserver = null } } }
在ContentResolver 中用到了这个类:
public final void registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer) { registerContentObserver(uri, notifyForDescendents, observer, UserHandle.myUserId()); }
标签:
原文地址:http://www.cnblogs.com/moor/p/4722470.html