【问题标题】:Fetching Phone Number from Contacts based on Name Android根据名称Android从联系人中获取电话号码
【发布时间】:2012-04-26 05:34:37
【问题描述】:

我正在开发一个应用程序,我需要从与提供的姓名对应的联系人中获取电话号码。我尝试了很多代码,但似乎都没有。

这是我现在正在使用的代码

public static String getContactPhoneNumber(Context context, String contactName, int type) {
        String phoneNumber = null;

        String[] whereArgs = new String[] { contactName, String.valueOf(type) };

        Log.d(TAG, String.valueOf(contactName));

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.Contacts.DISPLAY_NAME + " = ? and "
                        + ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null);

        int phoneNumberIndex = cursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

        Log.d(TAG, String.valueOf(cursor.getCount()));

        if (cursor != null) {
            Log.v(TAG, "Cursor Not null");
            try {
                if (cursor.moveToNext()) {
                    Log.v(TAG, "Moved to first");
                    Log.v(TAG, "Cursor Moved to first and checking");
                    phoneNumber = cursor.getString(phoneNumberIndex);
                }
            } finally {
                Log.v(TAG, "In finally");
                cursor.close();
            }
        }

        Log.v(TAG, "Returning phone number");
        return phoneNumber;
    }

在传递联系人姓名(例如:John Doe)和类型(2 是移动类型的 int 值)时,即使联系人“John Doe”存在于我的联系人列表中,返回的电话号码也是 null。

请帮忙!!!

【问题讨论】:

    标签: android contacts fetch phone-number


    【解决方案1】:

    试试这个

    而不是ContactsContract.CommonDataKinds.Phone.CONTENT_URI 通过 ContactsContract.Contacts.CONTENT_URI 查询方法的参数。

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 2015-04-01
      • 1970-01-01
      • 2014-11-01
      • 2014-05-26
      • 2017-04-13
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多