号码中的联系人 ID

发布于 2025-01-07 09:26:39 字数 1814 浏览 1 评论 0原文

我可以从她的 ID 中找到联系人的电话号码。以下代码片段在屏幕上打印一个数字。

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
            null, null);
    phones.moveToNext();
    String phoneNumber = phones.getString(
            phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    Toast.makeText(getBaseContext(), phoneNumber, Toast.LENGTH_LONG).show();

但是,如果我尝试从她的号码中找到她的 ID,

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
            null, null);
    phones.moveToNext();
    String phoneNumber = phones.getString(
            phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //Now I got a valid phone number


            //using that number to find the id
    Cursor ids = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber,
            null, null);
    ids.moveToNext();
    String id = ids.getString(
            ids.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
    Toast.makeText(getBaseContext(), id, Toast.LENGTH_LONG).show();

则会收到以下错误。

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

我使用的投影如下:

private static final String[] CONTACT_PROJECTION = new String[] {
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    ContactsContract.CommonDataKinds.Phone._ID
};

即使我知道联系人中有这样的条目,第二个光标也是空的。我做错了什么?

I can find the number of the contact from her id. The following snippet prints a number on the screen.

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
            null, null);
    phones.moveToNext();
    String phoneNumber = phones.getString(
            phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    Toast.makeText(getBaseContext(), phoneNumber, Toast.LENGTH_LONG).show();

However, if I try to find her id from her number,

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
            null, null);
    phones.moveToNext();
    String phoneNumber = phones.getString(
            phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //Now I got a valid phone number


            //using that number to find the id
    Cursor ids = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber,
            null, null);
    ids.moveToNext();
    String id = ids.getString(
            ids.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
    Toast.makeText(getBaseContext(), id, Toast.LENGTH_LONG).show();

I get the following error.

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

The projection I use is as follows:

private static final String[] CONTACT_PROJECTION = new String[] {
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    ContactsContract.CommonDataKinds.Phone._ID
};

The second cursor is empty even if I know there is such an entry in the contacts. What am I doing wrong?

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

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

发布评论

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

评论(1

萌吟 2025-01-14 09:26:39

不知道为什么,但 PhoneLookup 对我有用。对于那些有同样问题的人,这是我的代码。

Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("aNumber"));
Cursor phones = getContentResolver().query(contactUri , PEOPLE_PROJECTION, null, null, null);
//here, the cursor keeps the wanted contact

...

private static final String[] PEOPLE_PROJECTION = new String[] {
    ContactsContract.PhoneLookup._ID
};

Don't know why, but PhoneLookup worked for me. For those who have the same problem, here is my code.

Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("aNumber"));
Cursor phones = getContentResolver().query(contactUri , PEOPLE_PROJECTION, null, null, null);
//here, the cursor keeps the wanted contact

...

private static final String[] PEOPLE_PROJECTION = new String[] {
    ContactsContract.PhoneLookup._ID
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文