从查找URI获取电话号码电话号码、URI

2023-09-06 10:40:00 作者:你是我的无可替代

我一直试图利用自己的查询URI来获取联系人的电话号码,但我没有得到它的工作。

I've been trying to obtain a contact's phone number using their lookup URI, but I'm not getting it to work.

Cursor myC = getContentResolver().query(lookupURI, null, null,
                        null, null);
                String phoneNumber;
                if (myC.moveToFirst()) {
                    while (myC.moveToNext()) {
                        phoneNumber = myC.getString(myC
                                .getColumnIndex(Phone.NUMBER));
                        Log.v("t", "phone number is: " + phoneNumber);
                    }
                }

其中, lookupURI.toString()是这样的URI: content://com.android.contacts/contacts/lookup/0r1-304846522C3052482C4A3442423C3248/1

where lookupURI.toString() is this URI: content://com.android.contacts/contacts/lookup/0r1-304846522C3052482C4A3442423C3248/1

任何人都知道我在做什么错了?

Anyone knows what I'm doing wrong?

推荐答案

不能保证这一点要工作4.0,因为我没有用它在一段时间,但在2.3.3正常工作:

Can't guarantee this'll work for 4.0 because I haven't used it in a while but works fine on 2.3.3:

要得到的ContactID,我第一次得到了用户选择一个联系人:

To get the contactId, I first get the user to select a contact:

public void clickSelectContact(View v) {
    Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(i, CONTACTS_REQUEST_CODE);
}

当用户已经选择了一个接触它回来该方法

When the user has selected a contact it comes back to this method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CONTACTS_REQUEST_CODE){
        if(resultCode == RESULT_OK){
             Uri uri = data.getData();
             System.out.println("uri: "+uri);
             System.out.println("PHONE NUMBER: " +  PhoneUtils.getContactPhoneNumber(this, uri.getLastPathSegment()));
        }
    }
}

这就要求我的静态的Util类:

Which calls my static util class:

private static final String TAG = "PhoneUtils";

public static String getContactPhoneNumber(Context context, String contactId) {
   int type = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
   String phoneNumber = null;

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

   Log.d(TAG, "Got contact id: "+contactId);

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

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

  if (cursor != null) {
       Log.d(TAG, "Returned contact count: "+cursor.getCount());
       try {
             if (cursor.moveToFirst()) {
                 phoneNumber = cursor.getString(phoneNumberIndex);
             }
            } finally {
               cursor.close();
            }
   }

  Log.d(TAG, "Returning phone number: "+phoneNumber);
  return phoneNumber;
}

其中的ContactID = lookupURI.getLastPathSegment();

Where contactId = lookupURI.getLastPathSegment();

因此​​,对于这样一个简单的事情复杂! : - (

So complex for such a simple thing! :-(

P.S。你可能需要这个权限在你的清单:

P.s. you may need this permission in your manifest:

 <uses-permission android:name="android.permission.READ_CONTACTS" />