从 RawContact Android 读取电话号码
Posted
技术标签:
【中文标题】从 RawContact Android 读取电话号码【英文标题】:Read a Phone Number from RawContact Android 【发布时间】:2014-09-05 23:48:30 【问题描述】:我想检索 whatsapp 上的联系人电话号码。 我能够读取联系人的姓名,但无法获取电话号码。 这是我的代码
void readwhatsapp()
StringBuffer output = new StringBuffer();
ContentResolver cr1 = getContentResolver();
Cursor c = cr1.query(
RawContacts.CONTENT_URI,
new String[] RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY,RawContacts.PHONETIC_NAME ,
RawContacts.ACCOUNT_TYPE + "= ?",
new String[] "com.whatsapp" ,
null);
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
int contactIDColumn = c.getColumnIndex(RawContacts.CONTACT_ID);
while (c.moveToNext())
// You can also read RawContacts.CONTACT_ID to read the
// ContactsContract.Contacts table or any of the other related ones.
output.append(c.getString(contactNameColumn));
output.append(" ");
//getContactAccount(c.getString(contactIDColumn),cr1);
outputText.setText(output);
【问题讨论】:
【参考方案1】:使用以下代码,它适用于我的自定义帐户同步。我将 myaccount 名称更改为 whatsapp。我希望它有效..
String[] projection = new String[]
RawContacts._ID, RawContacts.DISPLAY_NAME_PRIMARY, ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.NUMBER;
Cursor people = mContentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, RawContacts.ACCOUNT_TYPE + "= ?",
new String[] "com.whatsapp" , null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int indexUid=people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
if(people != null && people.moveToFirst())
String name = people.getString(indexName);
String number2 = people.getString(indexNumber);
String uid=people.getString(indexUid);
Log.d("name=",name);
Log.d("number=",number2);
Log.d("unique id=",uid);
while (people.moveToNext());
people.close();
【讨论】:
以上是关于从 RawContact Android 读取电话号码的主要内容,如果未能解决你的问题,请参考以下文章