android获取手机联系人信息

/*
     * 手机联系人定义常量
     */
    public static final String[] PHONES_PROJECTION = new String[]
    {
            Phone.DISPLAY_NAME, //联系人姓名
            Phone.NUMBER,       //电话号码
            Photo.PHOTO_ID,     //联系人头像
            Phone.CONTACT_ID    //ID
    };
   
    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;


/**
     * 获取联系人信息
     *
     * @param
     * @return void
     * @throws
     * @since TianTian
     */
    private void getContactInformation()
    {
        Uri contact_uri = Phone.CONTENT_URI; //获得联系人默认uri
        ContentResolver resolver = this.getContentResolver();  //获得ContentResolver对象
        Cursor cursor = resolver.query(contact_uri,Constant.PHONES_PROJECTION, null, null, null); //获取电话本中开始一项光标
        contacts = new ArrayList<ContactPeson>();
        if (null != cursor)
        {
            while (cursor.moveToNext())
            {
                ContactPeson peson = new ContactPeson();
               
                /*
                 * 获取电话号码
                 */
                String number = cursor.getString(Constant.PHONES_NUMBER_INDEX);
                /*
                 *  当手机号码为空的或者为空字段 跳过当前循环
                 */
                if (TextUtils.isEmpty(number)) continue;
               
                /*
                 *  得到联系人名称
                 */
                String name = cursor.getString(Constant.PHONES_DISPLAY_NAME_INDEX);
                /*
                 * 得到联系人ID 
                 */
                Long id = cursor.getLong(Constant.PHONES_CONTACT_ID_INDEX); 
                /*
                 * 得到联系人头像ID 
                 */
                Long photo = cursor.getLong(Constant.PHONES_PHOTO_ID_INDEX); 
                Bitmap contactPhoto = null; 
                if( photo > 0 )
                { 
                    Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); 
                    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri); 
                    contactPhoto = BitmapFactory.decodeStream(input); 
                }
                else
                { 
                    contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.jt3); 
                } 
               
                peson.setContact_number(number);
                peson.setContact_name(name);
                peson.setContact_photo(contactPhoto); 
               
                contacts.add(peson);
               
            }
           
            cursor.close();
        }
    
    }


model:

private String contact_id;
   
    private Bitmap contact_photo; //联系人照片
   
    private String contact_name; //联系人名字
   
    private String contact_number; //联系人电话号码

猜你喜欢

转载自mickey-hou.iteye.com/blog/1613154