Example usage for android.database ContentObserver onChange

List of usage examples for android.database ContentObserver onChange

Introduction

In this page you can find the example usage for android.database ContentObserver onChange.

Prototype

public void onChange(boolean selfChange) 

Source Link

Document

This method is called when a content change occurs.

Usage

From source file:com.android.email.EmailNotificationController.java

/**
 * Registers an observer for changes to mailboxes in the given account.
 * NOTE: This must be called on the notification handler thread.
 * @param accountId The ID of the account to register the observer for. May be
 *                  {@link Account#ACCOUNT_ID_COMBINED_VIEW} to register observers for all
 *                  accounts that allow for user notification.
 *///from w ww .j  a v a2  s . c  o  m
private void registerMessageNotification(final long accountId) {
    ContentResolver resolver = mContext.getContentResolver();
    if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
        Cursor c = resolver.query(Account.CONTENT_URI, EmailContent.ID_PROJECTION, null, null, null);
        try {
            while (c.moveToNext()) {
                long id = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
                registerMessageNotification(id);
            }
        } finally {
            c.close();
        }
    } else {
        ContentObserver obs = mNotificationMap.get(accountId);
        if (obs != null)
            return; // we're already observing; nothing to do
        LogUtils.i(LOG_TAG, "Registering for notifications for account " + accountId);
        ContentObserver observer = new MessageContentObserver(sNotificationHandler, mContext, accountId);
        resolver.registerContentObserver(Message.NOTIFIER_URI, true, observer);
        mNotificationMap.put(accountId, observer);
        // Now, ping the observer for any initial notifications
        observer.onChange(true);
    }
}

From source file:com.tct.email.NotificationController.java

/**
 * Registers an observer for changes to mailboxes in the given account.
 * NOTE: This must be called on the notification handler thread.
 * @param accountId The ID of the account to register the observer for. May be
 *                  {@link Account#ACCOUNT_ID_COMBINED_VIEW} to register observers for all
 *                  accounts that allow for user notification.
 *///from  w w  w. j  a  v  a  2s .  c om
private void registerMessageNotification(final long accountId) {
    ContentResolver resolver = mContext.getContentResolver();
    if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
        Cursor c = resolver.query(Account.CONTENT_URI, EmailContent.ID_PROJECTION, null, null, null);
        try {
            //TS: junwei-xu 2015-12-30 EMAIL BUGFIX-1128322 MOD_S
            while (c != null && c.moveToNext()) {
                long id = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
                registerMessageNotification(id);
            }
            //TS: junwei-xu 2015-12-30 EMAIL BUGFIX-1128322 MOD_E
        } finally {
            //TS: jian.xu 2015-12-08 EMAIL BUGFIX-1118361 MOD_S
            if (c != null) {
                c.close();
            }
            //TS: jian.xu 2015-12-08 EMAIL BUGFIX-1118361 MOD_E
        }
    } else {
        ContentObserver obs = mNotificationMap.get(accountId);
        if (obs != null)
            return; // we're already observing; nothing to do
        LogUtils.i(LOG_TAG, "Registering for notifications for account " + accountId);
        ContentObserver observer = new MessageContentObserver(sNotificationHandler, mContext, accountId);
        resolver.registerContentObserver(Message.NOTIFIER_URI, true, observer);
        mNotificationMap.put(accountId, observer);
        // Now, ping the observer for any initial notifications
        observer.onChange(true);
    }
}