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");