Android 联系人选择器不返回电话号码,但姓名?

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

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

最佳答案

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

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 变量。享受吧!

关于Android 联系人选择器不返回电话号码,但姓名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7766671/

相关文章:

安卓SDK : source code of Contact ContentProvider

java - Android 检索联系人数据时出现 RuntimeException

android - Google Play 服务库错误信号

java - Android 除法始终结果为 0

java - 自定义 ListView 没有响应 itemClickListener

android - 修改联系方式

android - 每当将新联系人添加到“联系人”时,如何添加类似 whatsapp 的选项以联系

java - Android - 类型 ID 的预期资源

android - 自定义键盘无法在 fragment 中工作

android - 从android中的电话簿中选择一个联系人