在 Android 中从 URI 中检索联系电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3370628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve Contact Phone Number From URI in Android
提问by hwrdprkns
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.
在我从内置活动中检索他们的 ID 号后,我试图获取联系人的电话号码。但是,每当我使用下面代码中的光标查询数据库时,即使我选择的联系人有手机号码,我也会返回零行。
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?
任何人都可以为我指出更好的方向或展示如何在获取用户 ID 后获取联系人电话号码的示例?
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)
从中我将 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;
}
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?
它为电话号码返回 null - 这意味着它找不到我试图访问的行 - 这意味着我的查询有问题 - 但是,如果我检查具有手机号码的联系人 - 如何我可以得到 0 行查询吗?
Any help would be greatly appreciated. Thank you so much!
任何帮助将不胜感激。非常感谢!
回答by hwrdprkns
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."
“此数据所属的 Contacts 表中行的 ID。”
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.
因为无论如何我都是从联系人表中获取 URI - 这不是必需的,并且应该替换以下内容。ID 是与电话表中的联系人对应的 ID,而不是原始联系人。
ContactsContract.CommonDataKinds.Phone._ID
Exchanging the lines returned the correct results in the query. Everything seems to be working well at the moment.
交换行在查询中返回了正确的结果。目前一切似乎都运行良好。
回答by Pentium10
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.
电话号码存储在自己的表中,需要单独查询。要查询电话号码表,请使用存储在 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();
}
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.
对 Android 联系人 SQLite 数据库执行第二次查询。根据存储在 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 中的 URI 查询电话号码。联系人 ID 作为 ContactsContract.CommonDataKinds.Phone.CONTACT_ID 存储在电话表中,WHERE 子句用于限制返回的数据。