在 Android 中从 URI 中检索联系人电话号码 [英] Retrieve Contact Phone Number From URI in Android
问题描述
在我从内置活动中检索他们的 ID 号后,我正在尝试获取联系人的电话号码.但是,每当我在下面的代码中使用光标查询数据库时,即使我选择的联系人有手机号码,我也会返回零行.
谁能为我指出更好的方向或展示如何在获取用户 ID 后获取联系人电话号码的示例?
我的代码:
private Runnable getSMSRunnable() {返回新的 Runnable() {公共无效运行(){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) {字符串电话号码 = 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,空值,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? and "+ ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null);int phoneNumberIndex = 游标.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);Log.d(TAG, String.valueOf(cursor.getCount()));如果(光标!= null){Log.v(TAG, "Cursor Not null");尝试 {如果 (cursor.moveToNext()) {Log.v(TAG, "移到第一个");Log.v(TAG, "光标移到第一个并检查");phoneNumber = cursor.getString(phoneNumberIndex);}} 最后 {Log.v(TAG, "最后");游标.关闭();}}Log.v(TAG, "返回电话号码");返回电话号码;}
它为电话号码返回空值——这意味着它找不到我试图访问的行——这意味着我的查询有问题——但是如果我检查一个有手机号码的联系人-- 我怎么能得到一个 0 行的查询?
任何帮助将不胜感激.非常感谢!
我找到了答案.
我没有从游标中获取任何行的原因是因为我使用了该行
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
<块引用>
此数据所属的 Contacts 表中行的 id."
因为无论如何我都是从联系人表中获取 URI - 这不是必需的,并且应该替换以下内容.该 ID 是与电话表中的联系人对应的 ID,而不是原始联系人.
ContactsContract.CommonDataKinds.Phone._ID
交换行在查询中返回正确的结果.目前一切似乎都运行良好.
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!
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.
这篇关于在 Android 中从 URI 中检索联系人电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!