5

Does anyone know if there is a way to add a contact via adb or monkeyrunner?

Using

device.shell("am start -a android.intent.action.ACTION_INSERT -t ContactsContract.Contacts.CONTENT_TYPE -e ContactsContract.Intents.Insert.NAME "+name+" ContactsContract.Intents.Insert.PHONE "+number)

does not work for me

n8schloss
  • 2,723
  • 2
  • 19
  • 27

1 Answers1

12

Use something like this:

device.shell("am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name 'Donald Duck' -e phone 555-1234")
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • @dtmilano Thanks, it worked for me, too!But I have 2 similar questions: 1) how to delete a contact (just use `android.intent.action.DELETE`?)? 2) how to delete ALL Android contacts at once? – evgeny9 Dec 13 '12 at 15:29
  • @dtmilano Found a way to delete all contacts: `adb shell pm clear com.android.providers.contacts`. But then Contacts Sync should be turned off (and I could not find a way to do that from command line). – evgeny9 Dec 14 '12 at 15:50
  • A [AndroidViewClient](https://github.com/dtmilano/AndroidViewClient) script to disable Contacts Sync for an Account it would not be that difficult. Give it a try and create a new question if you find any problems. – Diego Torres Milano Dec 14 '12 at 16:09
  • So did you find a way to erase a specific contact? Furthermore, is there a way to erase all contacts without having to restart? For my case, Contacts app always crashes after erasing until a restart has been done! – Vladimir S. Jul 11 '13 at 10:25
  • If you (reader) are looking for a way to "Add and Save a Contact" with adb and Android Intents - you need to know that there is no way to do it. This answer works, but it does what an Android Intent is suppose to do - it launches an App ("Phone") with a particular screen open ("Add a Contact") with some details already in place (name='Donald Duck', phone='555-1234'), but it leaves the rest to a user. And from there it depends on: the default app/OS version/manufacturer/etc. So, as a result - you can use adb and Intent to launch something, but you cannot use it to fully complete the action. – Roman Roshchin May 26 '20 at 19:40