package com.example.myapi.phonepersion; import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.RawContacts; import android.provider.ContactsContract.RawContacts.Data; import android.util.Log; import com.example.myapi.bean.Person; /** * 获取手机通讯录联系人,帮助类 * @author tony * */ public class PhonePersionUtils { private PhonePersionUtils(){} private static PhonePersionUtils instance; public static PhonePersionUtils getInstance(){ synchronized (PhonePersionUtils.class) { if(instance == null){ instance = new PhonePersionUtils(); } } return instance; } /** * 获取所有的联系人 * @return */ public List<Person> getAllPerson(Context context){ List<Person> ps = new ArrayList<Person>(); try{ Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if(cur!=null){ while(cur.moveToNext()){ Person p = new Person(); String phoneNum = cur.getString(cur.getColumnIndex(Phone.NUMBER)); String name = cur.getString(cur.getColumnIndex(Phone.DISPLAY_NAME)); p.setName(name); p.setPhone(phoneNum); ps.add(p); } } cur.close(); }catch(Exception e){ Log.e("getAllPerson", e.getMessage()); } return ps; } /** * 获取SIM卡中的联系人 * @param context */ public List<Person> getSIMContacts(Context context) { List<Person> ps = new ArrayList<Person>(); ContentResolver resolver = context.getContentResolver(); // 获取Sims卡联系人 Uri uri = Uri.parse("content://icc/adn"); Cursor cur = resolver.query(uri, null,null,null,null); if (cur != null) { while (cur.moveToNext()) { Person p = new Person(); String phoneNum = cur.getString(cur.getColumnIndex(Phone.NUMBER)); String name = cur.getString(cur.getColumnIndex(Phone.DISPLAY_NAME)); p.setName(name); p.setPhone(phoneNum); ps.add(p); } cur.close(); } return ps; } /** * 向手机通讯录中添加联系人 * @return */ public void add(Context context){ ContentValues values = new ContentValues(); // 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的raw_id Uri uri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values); long raw_id = ContentUris.parseId(uri); // 往data表入姓名数据 values.clear(); values.put(Data.RAW_CONTACT_ID, raw_id); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型 values.put(StructuredName.GIVEN_NAME, "yw"); context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI,values); // 往data表入电话数据 values.clear(); values.put(Data.RAW_CONTACT_ID, raw_id); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);// 内容类型 values.put(Phone.NUMBER, "13737476757"); values.put(Phone.TYPE, Phone.TYPE_MOBILE);context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI,values); // 往data表入Email数据 values.clear(); values.put(Data.RAW_CONTACT_ID, raw_id); values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);// 内容类型 values.put(Email.DATA, "3747678797@xxx.com"); values.put(Email.TYPE, Email.TYPE_WORK); context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI,values); } }
需要添加的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
希望对大家有帮助。