Android 通讯录存入手机电话邮箱地址等信息
Posted QXXXD
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 通讯录存入手机电话邮箱地址等信息相关的知识,希望对你有一定的参考价值。
存储 工具类
- tips: 在存储入手机通讯录之前,需要获取手机通讯录运行时权限
创建想要存入信息的
ContentProviderOperation
实体,并填入对应信息和必填的ID
,添加到ArrayList中,再使用ContentResolver
的applyBatch
方法批量操作。
public static void InsertContact(Context context, ContactBean contact)
ContentValues values = new ContentValues();
Uri rawContactUri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = context.getContentResolver();
// 生成 id
long rawContactId = ContentUris.parseId(resolver.insert(rawContactUri, values));
// 插入数据
rawContactUri = Uri.parse("content://com.android.contacts/data");
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
// 写入名字
if (null != contact.getName() && !"".equals(contact.getName()))
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.getName())
.build());
// 写入手机号
if (null != contact.getMobile())
for (MobileBean mobile : contact.getMobile())
if (null != mobile.getContent() && !"".equals(mobile.getContent()))
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobile.getContent())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
// 座机
if (null != contact.getPhone())
for (PhoneBean phone : contact.getPhone())
if (null != phone.getContent() && !"".equals(phone.getContent()))
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone.getContent())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());
// 邮箱
if (null != contact.getEmail())
for (EmailBean email : contact.getEmail())
if (null != email.getContent() && !"".equals(email.getContent()))
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email.getContent())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
// 公司
if (null != contact.getCompany())
for (CompanyBean company : contact.getCompany())
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK);
if (null != company.getComname() && !"".equals(company.getComname()))
builder.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company.getComname());
if (null != company.getZhiwu() && !"".equals(company.getZhiwu()))
builder.withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, company.getZhiwu());
builder.withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, company.getZhiwu());
builder.withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, company.getZhiwu());
builder.withValue(ContactsContract.CommonDataKinds.Organization.PHONETIC_NAME, company.getZhiwu());
builder.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, company.getZhiwu());
ops.add(builder.build());
// 地址
if (null != contact.getAddress() && !"".equals(contact.getAddress()))
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contact.getAddress())
.build());
try
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
catch (OperationApplicationException | RemoteException e)
e.printStackTrace();
手机号验重工具类
/**
* 判断某个手机号是否存在
*/
private boolean isPhoneExist(Context context, String phoneNum)
Cursor cursor = null;
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNum);
ContentResolver resolver = context.getContentResolver();
cursor = resolver.query(uri, new String[]ContactsContract.Data.DISPLAY_NAME,
null, null, null);
if (cursor.moveToFirst())
cursor.close();
return true;
cursor.close();
return false;
开发者涨薪指南
48位大咖的思考法则、工作方式、逻辑体系
以上是关于Android 通讯录存入手机电话邮箱地址等信息的主要内容,如果未能解决你的问题,请参考以下文章