从查找 URI 获取电话号码

发布于 2024-12-22 03:41:24 字数 700 浏览 3 评论 0原文

我一直在尝试使用联系人的查找 URI 获取联系人的电话号码,但没有成功。

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

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

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

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

Anyone knows what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2024-12-29 03:41:24

不能保证这适用于 4.0,因为我有一段时间没有使用它了,但在 2.3.3 上运行良好:

为了获取 contactId,我首先让用户选择一个联系人:

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

当用户选择了一个联系人时它回到这个方法:

@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 类:

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

这么简单的事情竟然这么复杂! :-(

Ps,您可能需要在清单中获得此许可:

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

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:

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

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

Where contactId = lookupURI.getLastPathSegment();

So complex for such a simple thing! :-(

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

 <uses-permission android:name="android.permission.READ_CONTACTS" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文