在Android中检索联系人的电话号码的URI [英] Retrieve Contact Phone Number From URI in Android
问题描述
我试图让该联系人的电话号码,我已经取回自己的身份证号码,从内置的活动后。但是,每当我查询使用光标数据库下面我code - 我得到零行返回即使有一个手机号码,因为我选择了接触。
任何人都可以点我一个更好的方向或展示如何获取联系人的电话号码,让他们的用户ID后的例子吗?
我的code:
私人可运行getSMSRunnable(){
返回新的Runnable(){
公共无效的run(){
意图I =新的意图(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(我,CONTACTS_REQUEST_ code);
}
};
}
返回日志输出
内容://com.android.contacts/data/6802
从中我传递的ID(6802),其中的目的是从ID具有给定类型返回的电话号码的方法(在此情况下ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
公共静态字符串getContactPhoneNumberByPhoneType(上下文的背景下,长期的ContactID,整型){
字符串phoneNumber的= NULL;
的String [] whereArgs =新的String [] {将String.valueOf(的ContactID),将String.valueOf(类型)};
Log.d(TAG,将String.valueOf(的ContactID));
光标光标= context.getContentResolver()查询(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
空值,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?和
+ 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.moveToNext()){
Log.v(TAG,感动先);
Log.v(TAG,光标移到第一和检查);
phoneNumber的= cursor.getString(phoneNumberIndex);
}
} 最后 {
Log.v(TAG,在最后);
cursor.close();
}
}
Log.v(TAG,回归电话号码);
返回phoneNumber的;
}
这对于一个电话号码返回null - 这意味着它无法找到,我尝试访问该行 - 这意味着什么是错的我的查询 - 但是如果我检查,有一个手机号码的联系人 - 我怎么能得到一个0行查询
?任何帮助将是很大的AP preciated。太谢谢你了!
我找到了答案。
我之所以没有得到从光标任何行,因为我用的是行
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
在该数据所属的联系人表中的行的ID。
由于我是从联系人表中获取URI反正 - 这是没有必要的,应该已经取代以下。的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屋!