4

I am developing an Android app. In this app, I have to sync contacts with "Android Contacts".

I read some complicate writing about this subject. For example:

http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-2/

This is good example for sync contacts but I don't think create a new account for this. So decide sync contacts manually and search again. I found ContentObserver.

how to listen for changes in Contact Database

My question is:

public class DenemeObserverActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("MyTag", "OnCreate---");
    //this.getApplicationContext().getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contentObserver);


    Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    cursor.setNotificationUri(this.getApplicationContext().getContentResolver(), ContactsContract.Contacts.CONTENT_URI);
    this.getApplicationContext().getContentResolver().notifyChange(ContactsContract.Contacts.CONTENT_URI, null);
    cursor.registerContentObserver(contentObserver);


}

private class MyContentObserver extends ContentObserver {

    public MyContentObserver() {
        super(null);
    }

    @Override
    public boolean deliverSelfNotifications() {
        Log.d("MyTag", "DeliverSelfNotifications---");
        return true;
        }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Log.d("MyTag", "onChanges---");
    }

}


MyContentObserver contentObserver = new MyContentObserver(); 

In this code if I remove these lines:

Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cursor.setNotificationUri(this.getApplicationContext().getContentResolver(), ContactsContract.Contacts.CONTENT_URI);
this.getApplicationContext().getContentResolver().notifyChange(ContactsContract.Contacts.CONTENT_URI, null);
cursor.registerContentObserver(contentObserver);

and unmark: //this.getApplicationContext().getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contentObserver);

I can handle changes contacts changes. But Android just calling onChange method.

If a try with this code (with cursor) when contacts change, I can't see any call for onChange().

How can I handle "Which row was changed?".

OR

Can I use AbstractThreadedSyncAdapter for sync contacts without a new Account, if so how?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
sem
  • 61
  • 1
  • 9

0 Answers0