如何获取所有联系人及其所有属性

时间:2012-05-25 14:14:41

标签: android contacts

有没有人知道如何获取保存在Android设备上的所有联系人及其所有属性(姓名,电话,电子邮件......)?

我需要一个适用于Android 1.5到4.0的解决方案。

很多

1 个答案:

答案 0 :(得分:10)

获取姓名和号码使用此代码::

private void getNameNumber(){
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER };
        Cursor names = getContentResolver().query(uri, projection, null, null, null);

        int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        names.moveToFirst();
        do {

            String name   = names.getString(indexName);
            Log.e("Name new:", name);
            String number = names.getString(indexNumber);
            Log.e("Number new:","::"+number);

        } while (names.moveToNext());
    }

并获取所有细节使用以下代码并根据您的要求进行修改。

private void getDetails(){
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
            String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER ,ContactsContract.CommonDataKinds.Email.DATA };
        Cursor names = getContentResolver().query(uri, projection, null, null, null);

        int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        names.moveToFirst();
        do {

            String name   = names.getString(indexName);
            Log.e("Name new:", name);
            String number = names.getString(indexNumber);
            Log.e("Number new:","::"+number);

        } while (names.moveToNext());
        // email


            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor email = cr.query( 
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                                new String[]{id}, null); 
                while (email.moveToNext()) { 
                    //to get the contact names
                    // if the email addresses were stored in an array
                    String emailid = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email id ::", emailid);
                    String emailType = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
                    Log.e("Email Type ::", emailType);

                } 
                email.close();
            }


        //address
            while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID+ " = ?",
                    new String[] { id },null);
            while(addrCur.moveToNext()) {
                String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                Log.e("Street ::", street);
                String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                Log.e("City ::", city);
                String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                Log.e("State ::", state);
                String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                Log.e("Postal Code ::", postalCode);
            } 
            addrCur.close();

        }
    }