6

Can you please let me know if there is any adb command that can be used to find total number of contacts on device. -thanks in advance, Manju

0

4 Answers 4

12

this does not require root and sqlite:

adb shell content query --uri content://contacts/phones/  --projection display_name:number:notes  # google contacts?

adb shell content query --uri content://com.android.contacts/data --projection display_name:data1:data4:contact_id
2
  • 1
    I´m getting permission denial with that commands, "accessing provider:com.android.contactsjava.lang.SecurityException: Permission Denial: Do not have permission in call getContentProviderExternal() from pid=12044, uid=10101"
    – xanexpt
    Jun 10, 2020 at 21:03
  • Yet more compact: $ adb shell content query --uri content://contacts/phones/ --projection display_name:number | cut -f 3- -d " "
    – dzmanto
    Aug 28, 2021 at 10:47
5

There are a few adb commands that appear to work (as of on Android 10).

List the people in the contact list

adb shell content query --uri content://contacts/people/

Count people in contact list

Assuming you have a bash shell (Linux, OSX, or Windows Subsystem for Linux), you can pipe the output of those commands to wc -l to get the count.

adb shell content query --uri content://contacts/people/ | wc -l

Other interesting commands include:

List the phone numbers

adb shell content query --uri content://contacts/phones/

List the groups

adb shell content query --uri content://contacts/groups/

List group membership

adb shell content query --uri content://contacts/groupmembership/

List organizations

adb shell content query --uri content://contacts/organizations/
4

You can try the following code:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null, null,null);
while (cursor.moveToNext()) {
    listName.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
    listContactId.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
    if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {                
        Cursor pCur = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)),null, null);
        while (pCur.moveToNext()) {
            listMobileNo.add(pCur.getString(pCur.getColumnIndex("DATA1")));
        } 
        pCur.close();
    } else
        listMobileNo.add("");
}               

update


adb pull /data/data/com.android.providers.contacts/databases/contacts2.db <PATH TO PASTE THE DATABASE>

after retreiving the DB use SQLite Browser to open it

3
  • is there any adb command available to get count of contacts on android device ?
    – Manju
    Sep 19, 2014 at 6:48
  • 1
    phone not rooted !! permission denied !! c:\>adb pull /data/data/com.android.providers.contacts/databases/contacts2.db . failed to copy '/data/data/com.android.providers.contacts/databases/contacts2.db' to './contacts2.db': Permission denied
    – Manju
    Sep 19, 2014 at 6:59
  • you have to root then
    – Maveňツ
    Sep 19, 2014 at 6:59
2

To count all contacts on the device:

adb shell content query --uri content://com.android.contacts/contacts | wc -l

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.