Android: Get updated and deleted contact only
Asked Answered
S

1

6

I am developing an application in which i am working on Android Contacts and not able to move ahead. In app the need of application is that the contact which is updated should send to server or the contact which is deleted should send to server for sync.

I am using the contact service as:

public class ContactService extends Service {
    private int mContactCount;
    Cursor cursor = null;
    static ContentResolver mContentResolver = null;

    // Content provider authority
    public static final String AUTHORITY = "com.android.contacts";
    // Account typek
    public static final String ACCOUNT_TYPE = "com.example.myapp.account";
    // Account
    public static final String ACCOUNT = "myApp";

    // Instance fields
    Account mAccount;
    Bundle settingsBundle;

    @Override
    public void onCreate() {
        super.onCreate();
        // Get contact count at start of service
        mContactCount = getContactCount();      
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Get contact count at start of service
        this.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, mObserver);
        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    private int getContactCount() {
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                return cursor.getCount();
            } else {
                cursor.close();
                return 0;
            }
        } catch (Exception ignore) {
        } finally {
            cursor.close();
        }
        return 0;
    }

    private ContentObserver mObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
             this.onChange(selfChange, null);
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            new changeInContact().execute();
        }
    };

    public class changeInContact extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... arg0) {
            ArrayList<Integer> arrayListContactID = new ArrayList<Integer>();

            int currentCount = getContactCount();

            if (currentCount > mContactCount) {
                // Contact Added
            } else if (currentCount < mContactCount) {
                // Delete Contact               
            } else if (currentCount == mContactCount) {             
                // Update Contact
            }
            mContactCount = currentCount;
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            contactService = false;
        } // End of post
    }
}

The issues i am facing are as follows :

A: In the above code for getting the recently updated contact i need to check the Version of each contact from device with my database stored version of contacts. Which took much time for large amount of contacts.

B. For getting deleted contact i need to check that the data for the Raw id stored in my database is present in device or not. If not then the contact is deleted. It also take too much time to check whole contacts.

But the same thing contact refresh is done in whats app in very few seconds like 2 to three seconds...

EDIT : In the above code in following module :

if (currentCount > mContactCount) {
    // Contact Added
    Log.d("In","Add");
} else if (currentCount < mContactCount) {
    // Delete Contact               
    Log.d("In","Delete");
} else if (currentCount == mContactCount) {             
    // Update Contact
    Log.d("In","Update");
}

I put the log. So the update module is called many times, and also when i do add or delete that time too...

Please guide me and suggest me what to do to reduce the timing for the above tasks...

Softener answered 3/2, 2016 at 10:3 Comment(0)
U
3

use the below query to get all the deleted and updated contacts.

public static final String ACCOUNT_TYPE = "com.android.account.youraccounttype"
public static final String WHERE_MODIFIED = "( "+RawContacts.DELETED + "=1 OR "+
            RawContacts.DIRTY + "=1 ) AND "+RawContacts.ACCOUNT_TYPE+" = '"+ ACCOUNT_TYPE+"'";

c = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI,
                    null,
                    WHERE_MODIFIED,
                    null,
                    null);
Uncommitted answered 4/2, 2016 at 8:34 Comment(5)
Thank you for answer. But above answers gives me my app accounts contact deleted or updated. I need all contacts which are deleted or updated.Softener
Just remove the account type condition from the where if you are interested in all accountsUncommitted
I tried that too, but getting many deleted and updated numbers. The need is only latest updated or deleted number.Softener
The query will return all the updated and deleted until they are marked as synchronized by the belonging account type. If you are repeatedly getting the same numbers then something is wrong with contact synchronization of the belonging account type.Uncommitted
May be you can elaborate on what accounts you are using for your testing. Are they standard accounts like google, exchange or accounts added by custom applications which are likely to be less reliable. Overall RawContacts.DIRTY is the standard way to mark contacts as updated.Uncommitted

© 2022 - 2024 — McMap. All rights reserved.