Add new contact via intent with multiple phone num

2019-06-17 07:43发布

I want to add a new contact using ContactsContract.Intents.Insert. But the problem is that I don't know how many phone numbers can be. And as I understand I can pass only three phone numbers using PHONE, SECONDARY_PHONE, TERTIARY_PHONE constants. Is there any way to pass more than three numbers?

2条回答
做自己的国王
2楼-- · 2019-06-17 08:10

Extend Ov3r1oad answer with email:

ContentValues row = new ContentValues();
row.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
row.put(ContactsContract.CommonDataKinds.Email.ADDRESS, emailAddress);
row.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
data.add(row);
查看更多
【Aperson】
3楼-- · 2019-06-17 08:15

Found a solution. It consists in using ContentValues:

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();

//Filling data with phone numbers
for (int i = 0; i < numberOfPhones; i++) {
    ContentValues row = new ContentValues();
    row.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    row.put(Phone.NUMBER, PhonesNumberList.get(i));
    row.put(Phone.TYPE, Phone.TYPE_WORK);
    data.add(row);
}

contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, mName);
contactIntent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(contactIntent);
查看更多
登录 后发表回答