内容监听器ContentObserver

来源:互联网 发布:大数据涉及哪些技术 编辑:程序博客网 时间:2024/06/05 10:46
内容监听器ContentObserver
android.database.ContentObserver
构造函数
public ContentObserver (Handler handler)
 onChange() will happen on the provider Handler.
参数
handler     The handler to run onChange(boolean) on. 
主要函数
public boolean deliverSelfNotifications ()
Returns true if this observer is interested in notifications for changes made through the cursor the observer is registered with.
注:这个函数的使用还是puzzle.
public final void dispatchChange (boolean selfChange)
注:这个是为提供用handler执行onChange的一个接口。所以一般比没必要重载它。
public void onChange (boolean selfChange)
This method is called when a change occurs to the cursor that is being observed.
参数
selfChange     true if the update was caused by a call to commit on the cursor that is being observed. 
注1:这个就是我们需要重载的函数,在里面可以实现对内容改变的个性化响应。
源码如下:
package android.database;  
import android.os.Handler;
  /**
  * Receives call backs for changes to content. Must be implemented by objects which are added
  * to a {@link ContentObservable}.
  */
 public abstract class ContentObserver {
      private Transport mTransport;
      // Protects mTransport
      private Object lock = new Object();
      /* package */ Handler mHandler;
      private final class NotificationRunnable implements Runnable {
          private boolean mSelf;
          public NotificationRunnable(boolean self) {
             mSelf = self;
         }
          public void run() {
             ContentObserver.this.onChange(mSelf);
         }
     }
      private static final class Transport extends IContentObserver.Stub {
         ContentObserver mContentObserver;
          public Transport(ContentObserver contentObserver) {
             mContentObserver = contentObserver;
         }
          public boolean deliverSelfNotifications() {
             ContentObserver contentObserver = mContentObserver;
             if (contentObserver != null) {
                 return contentObserver.deliverSelfNotifications();
             }
             return false;
         }
          public void onChange(boolean selfChange) {
             ContentObserver contentObserver = mContentObserver;
             if (contentObserver != null) {
                 contentObserver.dispatchChange(selfChange);
             }
         }
          public void releaseContentObserver() {
             mContentObserver = null;
         }
     }
      /**
      * onChange() will happen on the provider Handler.
      *      * @param handler The handler to run {@link #onChange} on.
      */
     public ContentObserver(Handler handler) {
         mHandler = handler;
     }
      /**
      * Gets access to the binder transport object. Not for public consumption.
      *      * {@hide}
      */
     public IContentObserver getContentObserver() {
         synchronized(lock) {
             if (mTransport == null) {
                 mTransport = new Transport(this);
             }
             return mTransport;
         }
    }
      /**
      * Gets access to the binder transport object, and unlinks the transport object
      * from the ContentObserver. Not for public consumption.
      *
      * {@hide}
      */
     public IContentObserver releaseContentObserver() {
         synchronized(lock) {
             Transport oldTransport = mTransport;
             if (oldTransport != null) {
                 oldTransport.releaseContentObserver();
                 mTransport = null;
             }
             return oldTransport;
         }
     }
      /**
      * Returns true if this observer is interested in notifications for changes
      * made through the cursor the observer is registered with.
      */
     public boolean deliverSelfNotifications() {
         return false;
     }
      /**
      * This method is called when a change occurs to the cursor that
      * is being observed.
      *
      * @param selfChange true if the update was caused by a call to <code>commit</code> on the
      *  cursor that is being observed.
      */
     public void onChange(boolean selfChange) {}
      public final void dispatchChange(boolean selfChange) {
         if (mHandler == null) {
             onChange(selfChange);
         } else {
             mHandler.post(new NotificationRunnable(selfChange));
         }
     }
 }
注1:代码来源http://hi-android.info/src/android/database/ContentObserver.java.html
原创粉丝点击