Android联系人选择器没有返回电话号码,但是名字?

时间:2011-10-14 11:01:18

标签: android android-contacts

我使用它来从联系人选择器返回结果。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (resultCode == RESULT_OK) {
     switch (requestCode) {
     case CONTACT_PICKER_RESULT:
     Cursor cursor = null;
     ContentResolver cr = getContentResolver();

     try {
     Uri result = data.getData();
     Log.v(DEBUG_TAG, "Got a contact result: "
                            + result.toString());

        // get the contact id from the Uri
        String id = result.getLastPathSegment();
    cursor =  managedQuery(data.getData(), null, null, null, null);  
    cursor.moveToNext(); 
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
                      name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
                    number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));

       Log.v("ID", contactId + name);

     // query for phone number
     cursor = getContentResolver().query(Phone.CONTENT_URI,
                            null, Phone.CONTACT_ID + "=?", new String[] { id },
                            null);

           int phoneIdx = cursor.getColumnIndex(Phone.DATA);
    int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                            .StructuredName.FAMILY_NAME);
     int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                            .StructuredName.GIVEN_NAME);

                    // get the phone number
                    if (cursor.moveToFirst()) {
                        number = cursor.getString(phoneIdx);
                        lastName = cursor.getString(lastNameIdx);
                        firstName = cursor.getString(firstNameIdx);


                        Log.v(DEBUG_TAG, "Got number " + number);
                    } else {
                        Log.w(DEBUG_TAG, "No results");
                    }
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Failed to get phone number data", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }

                    if (number.length() == 0) {
                        Toast.makeText(this, "No phone number found for this contact.",
                                Toast.LENGTH_LONG).show();
                    }
                    if(lastName.length()==0) {
                        Toast.makeText(this, "No last name found for this contact.", 
                                Toast.LENGTH_LONG).show();
                    }
                    if(firstName.length()==0) {
                        Toast.makeText(this, "No first name found for this contact.", 
                                Toast.LENGTH_LONG).show();
                    }

                }

                break;
            }

        } else {
            Log.w(DEBUG_TAG, "Warning: activity result not ok");
        }

     ContactInfo.setText("Contact Name: "+name+ " Phone Number: "+number);
    }

此方法返回该用户的名称,但由于某种原因它不会返回电话号码。我该怎么做呢?获取电话号码的代码似乎无法正常工作。

2 个答案:

答案 0 :(得分:1)

试试这段代码,它对我有用

public static void getContacts(ContentResolver cr) {
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
    // read id
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        /** read names **/
        String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        /** Phone Numbers **/
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
            while (pCur.moveToNext()) {
                String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
        }
        pCur.close();

    }
}
}

记录displayNamenumbertypeStr个变量。享受!

答案 1 :(得分:0)

我修改了gt_buddy的答案,以便只选择1个联系人。这是代码:

Cursor cur = managedQuery(intent.getData(), null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// read id
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
/** read names **/
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/** Phone Numbers **/
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.i(TAG, "Display name:" + displayName + " Phone: " + typeStr + " Number: " + number);
}
pCur.close();
}
}