0

我想获取本地联系人的所有详细信息,并且我能够获取名字、姓氏、手机号码、家庭号码、工作号码。但是对于多个其他号码,我只得到一个其他号码。如何获取所有输入的其他数字?

Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        // Querying the table ContactsContract.Contacts to retrieve all the contacts
        Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        if(contactsCursor != null && contactsCursor.getCount() > 0){
            contactsCursor.moveToFirst();
            do {
                long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
                Uri dataUri = ContactsContract.Data.CONTENT_URI;

                // Querying the table ContactsContract.Data to retrieve individual items like
                // home phone, mobile phone, work email etc corresponding to each contact 
                Cursor dataCursor = getContentResolver().query(dataUri, null, ContactsContract.Data.CONTACT_ID + "=" + contactId +" AND "+ContactsContract.CommonDataKinds.Phone.TYPE_OTHER +"=7", null, null);


                if(dataCursor != null && dataCursor.getCount() > 0){
                    dataCursor.moveToFirst();
                    // Getting Display Name
                    displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));

                    do{
                        // Getting NickName
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
                            nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));

                        // Getting Phone numbers
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                            switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE : 
                                mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME : 
                                homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK : 
                                workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                break;  
                            case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER : 


                                otherPhone1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));



                                otherPhone2 = dataCursor.getString(dataCursor.getColumnIndex("data2"));
                                otherPhone3 = dataCursor.getString(dataCursor.getColumnIndex("data3"));
                                otherPhone4= dataCursor.getString(dataCursor.getColumnIndex("data4"));
                                otherPhone5 = dataCursor.getString(dataCursor.getColumnIndex("data5"));
                                Log.d("othertypes of numbersssssssss  ", "other types of numbersssss "+otherPhone1+" "+otherPhone2+" "+otherPhone3);

                                break;  
                            }
                        }
4

1 回答 1

0

尝试这个

字符串 id ,名称;内容解析器 cr = getContentResolver();

 String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,  null, null, null, sortOrder);



  if (cur.getCount() > 0) {


        while (cur.moveToNext()) {



            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

            name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.i(tag, "Id is "+ id+"\t Name is"+name);

        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){                      


                    Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);

// 第二个循环将检索特定联系人 id 的所有联系人号码 while (pCur.moveToNext()) {

            // Do something with phones



                   int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

                   String phn = pCur.getString(phNumber);

                   Log.i("phn number",  phn);



                } 

                pCur.close();

            }

            }

}
于 2013-09-11T07:27:09.913 回答