FileDocCategorySizeDatePackage
ContentObserver.javaAPI DocAndroid 5.1 API8362Thu Mar 12 22:22:10 GMT 2015android.database

ContentObserver

public abstract class ContentObserver extends Object
Receives call backs for changes to content. Must be implemented by objects which are added to a {@link ContentObservable}.

Fields Summary
private final Object
mLock
private Transport
mTransport
android.os.Handler
mHandler
Constructors Summary
public ContentObserver(android.os.Handler handler)
Creates a content observer.

param
handler The handler to run {@link #onChange} on, or null if none.


                          
       
        mHandler = handler;
    
Methods Summary
public booleandeliverSelfNotifications()
Returns true if this observer is interested receiving self-change notifications. Subclasses should override this method to indicate whether the observer is interested in receiving notifications for changes that it made to the content itself.

return
True if self-change notifications should be delivered to the observer.

        return false;
    
private voiddispatchChange(boolean selfChange, android.net.Uri uri, int userId)
Dispatches a change notification to the observer. Includes the changed content Uri when available and also the user whose content changed.

If a {@link Handler} was supplied to the {@link ContentObserver} constructor, then a call to the {@link #onChange} method is posted to the handler's message queue. Otherwise, the {@link #onChange} method is invoked immediately on this thread.

param
selfChange True if this is a self-change notification.
param
uri The Uri of the changed content, or null if unknown.
param
userId The user whose content changed.

        if (mHandler == null) {
            onChange(selfChange, uri, userId);
        } else {
            mHandler.post(new NotificationRunnable(selfChange, uri, userId));
        }
    
public final voiddispatchChange(boolean selfChange)
Dispatches a change notification to the observer.

If a {@link Handler} was supplied to the {@link ContentObserver} constructor, then a call to the {@link #onChange} method is posted to the handler's message queue. Otherwise, the {@link #onChange} method is invoked immediately on this thread.

param
selfChange True if this is a self-change notification.
deprecated
Use {@link #dispatchChange(boolean, Uri)} instead.

        dispatchChange(selfChange, null);
    
public final voiddispatchChange(boolean selfChange, android.net.Uri uri)
Dispatches a change notification to the observer. Includes the changed content Uri when available.

If a {@link Handler} was supplied to the {@link ContentObserver} constructor, then a call to the {@link #onChange} method is posted to the handler's message queue. Otherwise, the {@link #onChange} method is invoked immediately on this thread.

param
selfChange True if this is a self-change notification.
param
uri The Uri of the changed content, or null if unknown.

        dispatchChange(selfChange, uri, UserHandle.getCallingUserId());
    
public IContentObservergetContentObserver()
Gets access to the binder transport object. Not for public consumption. {@hide}

        synchronized (mLock) {
            if (mTransport == null) {
                mTransport = new Transport(this);
            }
            return mTransport;
        }
    
public voidonChange(boolean selfChange)
This method is called when a content change occurs.

Subclasses should override this method to handle content changes.

param
selfChange True if this is a self-change notification.

        // Do nothing.  Subclass should override.
    
public voidonChange(boolean selfChange, android.net.Uri uri)
This method is called when a content change occurs. Includes the changed content Uri when available.

Subclasses should override this method to handle content changes. To ensure correct operation on older versions of the framework that did not provide a Uri argument, applications should also implement the {@link #onChange(boolean)} overload of this method whenever they implement the {@link #onChange(boolean, Uri)} overload.

Example implementation:


// Implement the onChange(boolean) method to delegate the change notification to
// the onChange(boolean, Uri) method to ensure correct operation on older versions
// of the framework that did not have the onChange(boolean, Uri) method.
{@literal @Override}
public void onChange(boolean selfChange) {
onChange(selfChange, null);
}

// Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
{@literal @Override}
public void onChange(boolean selfChange, Uri uri) {
// Handle change.
}

param
selfChange True if this is a self-change notification.
param
uri The Uri of the changed content, or null if unknown.

        onChange(selfChange);
    
public voidonChange(boolean selfChange, android.net.Uri uri, int userId)
Dispatches a change notification to the observer. Includes the changed content Uri when available and also the user whose content changed.

param
selfChange True if this is a self-change notification.
param
uri The Uri of the changed content, or null if unknown.
param
userId The user whose content changed. Can be either a specific user or {@link UserHandle#USER_ALL}.
hide

        onChange(selfChange, uri);
    
public IContentObserverreleaseContentObserver()
Gets access to the binder transport object, and unlinks the transport object from the ContentObserver. Not for public consumption. {@hide}

        synchronized (mLock) {
            final Transport oldTransport = mTransport;
            if (oldTransport != null) {
                oldTransport.releaseContentObserver();
                mTransport = null;
            }
            return oldTransport;
        }