Search code examples
androidcontactscontractcalllog

How to get contacts with which the user talks often?


Is it possible using ContactsContract to get contacts with which the user talks often?

I know I can use the CallLog ContentProvider and try to figure that out, but I wanted to know if there is already a way to do it.


Solution

  • The number of times a contact has been contacted

    ContactsContract.Contacts.times_contacted
    
    
                static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.STARRED,
                    ContactsContract.Contacts.TIMES_CONTACTED,
                    ContactsContract.Contacts.CONTACT_PRESENCE,
                    ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.LOOKUP_KEY,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                };
    
                String name_to_search = "John Doe";
    
    
                Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, null, null, ContactsContract.Contacts.TIMES_CONTACTED);
                context.startManagingCursor(c);
    
                if (c.moveToNext())
                {
                    String id = c.getString(0);
                    ArrayList<String> phones = new ArrayList<String>();
    
                    Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                    while (pCur.moveToNext())
                    {
                        phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                        Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    } 
                    pCur.close();   
                }