来自号码的联系人 ID



我可以从她的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();

然而,如果我试图从她的号码中找到她的身份证

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

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

不知道为什么,但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
};

相关内容

  • 没有找到相关文章

最新更新