android中获取联系人相关信息


自己看...........


public static final int PHONES_DISPLAY_NAME_INDEX = 0;
    public static final int PHONES_NUMBER_INDEX = 1;
    public static final int PHONES_PHOTO_ID_INDEX = 2;
    public static final int PHONES_CONTACT_ID_INDEX = 3;

    public static final String[] PHONES_PROJECTION = new String[]
    {
Phone.DISPLAY_NAME,
Phone.NUMBER,
Photo.PHOTO_ID,
Phone.CONTACT_ID
    };
    public static ArrayList<MyContacts> mycontacts=null;
    //获取手机中的联系人信息
private void getPhoneContacts(Context mContext) { 

if(mycontacts==null){
mycontacts= new ArrayList<MyContacts>();
}
    ContentResolver resolver = mContext.getContentResolver(); 
     
    // 获取手机联系人 
    Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null); 
     
    if (phoneCursor != null) { 

    while (phoneCursor.moveToNext()) { 
        //得到手机号码 
        String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); 
        //当手机号码为空的或者为空字段跳过当前循环 
        if (TextUtils.isEmpty(phoneNumber)) 
            continue; 
        
        //得到联系人名称 
        String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX); 
        
        //得到联系人ID 
        Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX); 
     
        //得到联系人头像ID 
        Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX); 
        
        //得到联系人头像Bitamp 
        Bitmap contactPhoto = null; 
     
        //photoid 大于0 表示联系人有头像如果没有给此人设置头像则给他一个默认的 
        if(photoid > 0 ) { 
            Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid); 
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri); 
            contactPhoto = BitmapFactory.decodeStream(input); 
        }else { 
            contactPhoto = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.contact); 
        } 
        
        MyContacts myContact=new MyContacts(contactName, phoneNumber, contactPhoto);
       
        mycontacts.add(myContact);
     } 
        phoneCursor.close(); 
    }
   
 
  } 
/* 获得手机sim卡联系人信息
sim卡和手机本人 获取的方式类似 只是url有点不一样 ,须要注意的一点是 sim卡  是没有联系人头像的。 */
private void getSIMContacts(Context mContext) { 
if(mycontacts==null){
mycontacts= new ArrayList<MyContacts>();
}
    ContentResolver resolver = mContext.getContentResolver(); 
    // 获取Sims卡联系人 
    Uri uri = Uri.parse("content://icc/adn"); 
    Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null, 
        null); 
    Bitmap contactPhoto = null; 
    if (phoneCursor != null) { 
        while (phoneCursor.moveToNext()) { 
       
     
        // 得到手机号码 
        String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); 
        // 当手机号码为空的或者为空字段 跳过当前循环 
        if (TextUtils.isEmpty(phoneNumber)) 
            continue; 
        // 得到联系人名称 
        String contactName = phoneCursor 
            .getString(PHONES_DISPLAY_NAME_INDEX); 
     
        //Sim卡中没有联系人头像  加載默認圖片
        contactPhoto = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.contact);
        MyContacts myContact=new MyContacts(contactName, phoneNumber, contactPhoto);
        mycontacts.add(myContact);
       } 
        phoneCursor.close(); 
    } 

你可能感兴趣的:(android中获取联系人相关信息)