从 Android 中的 URI 检索联系电话号码

发布于 2024-09-11 23:38:05 字数 2019 浏览 7 评论 0原文

从内置活动中检索联系人的 ID 号码后,我试图获取联系人的电话号码。但是,每当我在下面的代码中使用光标查询数据库时,即使我选择的联系人有手机号码,我也会返回零行。

任何人都可以为我指明更好的方向,或者展示如何在获取联系人的用户 ID 后获取联系人的电话号码的示例吗?

我的代码:

    private Runnable getSMSRunnable() {
    return new Runnable() {
    public void run() {
    Intent i = new Intent(Intent.ACTION_PICK,
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(i, CONTACTS_REQUEST_CODE);
   }
  };
 }

返回日志输出

content://com.android.contacts/data/6802

,我从中将 ID (6802) 传递到一个方法,该方法旨在从具有给定类型的 ID 返回电话号码(在本例中为 ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE),

public static String getContactPhoneNumberByPhoneType(Context context, long contactId, int type) {
    String phoneNumber = null;

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

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

    Cursor cursor = context.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? 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;
}

该方法返回 null电话号码 - 这意味着它找不到我正在尝试访问的行 - 这意味着我的查询有问题 - 但是,如果我检查具有手机号码的联系人 - 我怎样才能获得0行查询?

任何帮助将不胜感激。太感谢了!

I am trying to get the contact's phone number after I have retrieved their ID number from the built-in activity. However, whenever I query the database using the cursor in my code below -- I get zero rows returned even though there is a mobile number for the contact I have selected.

Can anyone point me in a better direction or show an example of how to get the contact's phone number AFTER getting their userID?

My code:

    private Runnable getSMSRunnable() {
    return new Runnable() {
    public void run() {
    Intent i = new Intent(Intent.ACTION_PICK,
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(i, CONTACTS_REQUEST_CODE);
   }
  };
 }

Returns the Log output

content://com.android.contacts/data/6802

From which i pass the ID (6802) into a method which is designed to return the phone number from the ID with the given type (in this case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)

public static String getContactPhoneNumberByPhoneType(Context context, long contactId, int type) {
    String phoneNumber = null;

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

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

    Cursor cursor = context.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? 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;
}

Which returns null for a phone number -- which means it cannot find the row that I am trying to access -- which means that something is wrong with my query -- however if I check a contact that has a mobile phone number -- how could I get a 0 row query?

Any help would be greatly appreciated. Thank you so much!

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

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

发布评论

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

评论(2

浅笑轻吟梦一曲 2024-09-18 23:38:05

我找到了答案。

我没有从光标中获取任何行的原因是因为我正在使用该行

ContactsContract.CommonDataKinds.Phone.CONTACT_ID

“此数据所属的联系人表中的行的 ID。”

因为无论如何我都是从联系人表中获取 URI —— 这不是必需的,并且应该替换以下内容。该 ID 是与电话表中的联系人相对应的 ID,而不是原始联系人。

ContactsContract.CommonDataKinds.Phone._ID

交换行在查询中返回了正确的结果。目前一切似乎都进展顺利。

I found the answer.

The reason I was not getting any rows from the cursor was because I was using the line

ContactsContract.CommonDataKinds.Phone.CONTACT_ID

"The id of the row in the Contacts table that this data belongs to."

Since I was getting the URI from contacts table anyways -- this was not needed and the following should have been substituted. The ID was the one corresponding to the contact in the phone table not the raw contact.

ContactsContract.CommonDataKinds.Phone._ID

Exchanging the lines returned the correct results in the query. Everything seems to be working well at the moment.

千仐 2024-09-18 23:38:05

这应该可行,(也许尝试丢失类型)

电话号码存储在自己的表中,需要单独查询。要查询电话号码表,请使用 SDK 变量 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 中存储的 URI。使用 WHERE 条件获取指定联系人的电话号码。

        if (Integer.parseInt(cur.getString(
               cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);
        while (pCur.moveToNext()) {
        // Do something with phones
        } 
        pCur.close();
    }

对 Android 联系人 SQLite 数据库执行第二次查询。根据 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 中存储的 URI 查询电话号码。联系人 ID 作为 ContactsContract.CommonDataKinds.Phone.CONTACT_ID 存储在电话表中,并且 WHERE 子句用于限制返回的数据。

This should work, (maybe try losing the type)

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

        if (Integer.parseInt(cur.getString(
               cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);
        while (pCur.moveToNext()) {
        // Do something with phones
        } 
        pCur.close();
    }

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

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