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