4

Actually, I have a table for which I am listing for changes using ContentObserver, and in OnChange(), I am triggering a SyncCall.

My requirement is to make a SyncCall only on insert or update operation and if the operation is delete, I dont want to make a SyncCall.

Registering a contentObserver for a table.

ContentResolver mResolver = context.getContentResolver();
    // Construct a URI that points to the content provider data table
    Uri mUri = HistoryDetailsContract.HistoryEntries.CONTENT_URI;
    /*
     * Create a content observer object.
     * Its code does not mutate the provider, so set
     * selfChange to "false"
     */
    TableContnetObserver observer = new TableContnetObserver(null);
    /*
     * Register the observer for the data table. The table's path
     * and any of its sub paths trigger the observer.
     */
    mResolver.registerContentObserver(mUri, true, observer);

My ContentObserver Class.

public class TableContnetObserver extends ContentObserver {

public TableContnetObserver(Handler handler) {
    super(handler);
}

/*
 * Define a method that's called when data in the
 * observed content provider changes.
 */
@Override
public void onChange(boolean selfChange, Uri changeUri) {
    /*
     * Ask the framework to run your sync adapter.
     * To maintain backward compatibility, assume that
     * changeUri is null.
     */
    //ContentResolver.requestSync(GenericAccountService.GetAccount(), HistoryDetailsContract.AUTHORITY, new Bundle());   
    SyncUtils.TriggerRefresh();
}

Is there a way to listen for only insert and update operations using ContentObserver?

Or At Least is there a way that I can know what type of operation(insert, update or delete) that triggered the onChange() method in COntentObserver Class?

Akhil
  • 189
  • 7

1 Answers1

1

ContentObserver by itself cannot know what operation caused the change. If you are the one sending the change notification, then you can either

  • Append something to the URI to indicate what the change was, e.g. a query parameter or fragment
  • Notify a different URI for different operations and listen for those also/instead.
Karakuri
  • 38,365
  • 12
  • 84
  • 104