3

I try to add a contact using the adb shell:

content insert --uri content://com.android.contacts/raw_contacts --bind deleted:i:0 --bind data1:s:'Bla Fffff' --bind data2:s:'Bla' --bind data3:s:'Fffff' --bind mimetype:s:'vnd.android.cursor.item/name' --bind contact_id:i:1 --bind raw_contact_id:i:1

The console does not show any error, but the command returns no output. Nevertheless, content query --uri content://com.android.contacts/raw_contacts returns No result found.

Not specifying the contact id or raw contact id does not help either.

I am trying to mimik the insertContact method from https://www.dev2qa.com/how-to-add-contact-in-android-programmatically/ using the adb shell.

How can I properly add a contact using the content tool? I am aware that contacts can be added via sending an intent, but I'd rather not use the UI for this purpose.

3
  • Which Android version and phone are you using?
    – Firelord
    Dec 27, 2018 at 13:10
  • Cubot J3, Android 8.1.0
    – user283096
    Dec 27, 2018 at 13:12
  • It does also not work for Google Pixel, Android 9
    – user283096
    Dec 27, 2018 at 16:07

1 Answer 1

2

I found myself needing to do just that. Here's how I managed to get the job done.

First, I create a raw contact by doing:

adb shell content insert --uri content://com.android.contacts/raw_contacts --bind account_type:s:SOME_ACCOUNT_TYPE --bind account_name:s:MY_ACCOUNT_NAME

This will effectively create a new Contact (you will be able to see a new contact in Android's Contact app) but with no extra information (phone, name, email, etc...)

Next, I add name information to the recently created contact:

adb shell content insert --uri content://com.android.contacts/data --bind raw_contact_id:i:1 --bind mimetype:s:vnd.android.cursor.item/name --bind data1:s:MY_CONTACT_NAME

After this, you will notice how the new contact now has a display name in Android's Contacts app.

Notice how, in this case, I need to pass the raw_contact_id of the contact I'm trying to set the name for. I used 1 just as an example.

You can query this information by checking the data from all contacts:

adb shell content query --uri content://com.android.contacts/raw_contacts

Or if you want the straight anwser:

adb shell content query --uri content://com.android.contacts/raw_contacts --projection _id --where "account_name=\'MY_ACCOUNT_NAME\'"

Finally, I add the phone information:

adb shell content insert --uri content://com.android.contacts/data --bind raw_contact_id:i:1 --bind mimetype:s:vnd.android.cursor.item/phone_v2 --bind data1:s:MY_PHONE_NUMBER --bind data2:s:SOME_PHONE_TYPE --bind data3:s:SOME_LABEL

And we are done! You can check Android's Contacts app and see a contact with a display name (MY_CONTACT_NAME) and a phone number (MY_PHONE_NUMBER). There's plenty more information you can add but I guess this covers your needs or at least gives you a good idea of how to continue.

Bonus: I know it isn't exactly on topic but you can even run a little program to automat this using UiAutomator. For example:

InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("content query --uri content://com.android.contacts/raw_contacts");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .