以编程方式添加一个具有多个号码的联系人
作者:互联网
如何以编程方式在Android手机上添加具有多个号码的一个联系人?
解决方法:
这就是我们可以做到的
将名称和数字数组传递给此方法.
public static void addToContactList(Context context, String strDisplayName, String[] strNumber) throws Exception {
ArrayList<ContentProviderOperation> cntProOper = new ArrayList<>();
int contactIndex = cntProOper.size();//ContactSize
ContentResolver contactHelper = context.getContentResolver();
cntProOper.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)//Step1
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
//Display name will be inserted in ContactsContract.Data table
cntProOper.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)//Step2
.withValueBackReference(android.provider.ContactsContract.Data.RAW_CONTACT_ID, contactIndex)
.withValue(android.provider.ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, strDisplayName) // Name of the contact
.build());
for (String s : strNumber) {
//Mobile number will be inserted in ContactsContract.Data table
cntProOper.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)//Step 3
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex)
.withValue(android.provider.ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, s) // Number to be added
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); //Type like HOME, MOBILE etc
}
ContentProviderResult[] s = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, cntProOper); //apply above data insertion into contacts list
for (ContentProviderResult r : s) {
Log.i(TAG, "addToContactList: " + r.uri);
}
}
标签:android,android-contacts 来源: https://codeday.me/bug/20191009/1881217.html