內容監聽器ContentObserver

來源:互聯網
上載者:User

內容監聽器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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.