从RawContact Android中读取电话号码

时间:2014-07-15 17:00:05

标签: android android-contacts

我想检索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 个答案:

答案 0 :(得分:0)

使用以下代码对我的自定义帐户同步起作用。我将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();