【问题标题】:Android Contact picker not returning phone number, but name?Android联系人选择器不返回电话号码,但姓名?
【发布时间】:2012-02-16 14:23:32
【问题描述】:

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

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);
    }

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

【问题讨论】:

  • phoneNumber 在数据库中的列索引等于 return int phoneIdx = cursor.getColumnIndex(Phone.DATA);?
  • 什么意思?你问它返回什么?我还不知道。
  • 它返回名称,所以我猜我的号码有问题。
  • 我正在询问返回 cursor.getColumnIndex(Phone.DATA);

标签: android android-contacts


【解决方案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 变量。享受吧!

【讨论】:

  • 我应该在我的 onActivityResult() 哪里插入这个?
  • 我给了你一个工作代码。您可以将其嵌入到 onActivityResult() 函数中。 (在您的 # case CONTACT_PICKER_RESULT: )或从那里调用此函数。这是你的选择......
  • ContentResolver 从何而来?我不明白它是如何访问选择的 cotack 的。
  • 好的,所以你的代码给了我所有的联系人。我只想要用户选择的那个。我有什么遗漏吗?
【解决方案2】:

我修改了 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();
}
}

【讨论】:

    猜你喜欢
    • 2011-08-10
    • 1970-01-01
    • 2016-01-02
    • 2017-10-06
    • 2020-03-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    相关资源
    最近更新 更多