Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232409
  • 博文数量: 54
  • 博客积分: 2656
  • 博客等级: 少校
  • 技术积分: 1020
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-19 21:06
文章分类

全部博文(54)

文章存档

2016年(3)

2014年(8)

2013年(4)

2012年(2)

2011年(29)

2010年(8)

我的朋友

分类: Java

2011-07-11 11:15:24

    private void getPhoneNumberById(String id) {
        //HashMap map = new HashMap();

        Cursor cursor = null;
        Cursor phones = null;
        try {
            cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "=" + id, null, null);
            if (cursor.moveToFirst()) {
                int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                Log.v("phoneCount", phoneCount+"");
                if (phoneCount > 0) {
                    // 获得联系人的电话号码

                    phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
                    if (phones.moveToFirst()) {
                        do {
                            HashMap<String, Object> map = new HashMap<String, Object>();
                            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            String phoneType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                            if (phoneType.equals("1")) {
                                // 家庭

                                map.put("type","家庭");
                            } else if (phoneType.equals("2")) {
                                // 手机

                                map.put("type","手机");
                            } else if (phoneType.equals("3")) {
                                // 单位

                                map.put("type","单位");
                            }else if(phoneType.equals("17")){
                                map.put("type","手机");
                            } else {
                                map.put("type","其他");
                            }
                            map.put("phoneNumber", phoneNumber);
                            Log.v("type-phoneNumber", phoneNumber+"-"+phoneType);
                            list.add(map);
                        } while (phones.moveToNext());
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception

        } finally {
            if (!cursor.isClosed() || cursor != null)
                cursor.close();
            if (!phones.isClosed() || phones != null)
                phones.close();
        }

    }

 


 

    private void getContactIdByPhoneNumber(String phoneNumber) {
        // if(null == phoneNumber) return;

        Cursor cur = null;
        try {
            cur = getContentResolver().query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID }, Data.DATA1 + "=" + phoneNumber, null, null);
            if (cur.moveToFirst()) {
                contactId = cur.getString(0);
                Log.v("id", contactId);
                getPhoneNumberById(contactId);
            }
        } catch (Exception e) {
            // TODO: handle exception

        } finally {
            if (!cur.isClosed() || cur != null)
                cur.close();
        }
    }

 


 

    private String getContactNameByPhoneNumber(String number) {
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        String[] projection1 = new String[] { PhoneLookup.DISPLAY_NAME };
        Cursor c = null;
        try {
            c = getContentResolver().query(uri, projection1, null, null, null);
            if (c.moveToFirst()) {
                return c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
            }
            return null;
        } catch (Exception e) {
            // TODO: handle exception

        } finally {
            if (!c.isClosed())
                c.close();
        }
        return null;

    }


阅读(975) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~