【问题标题】:Read a Phone Number from RawContact Android从 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);

 }

【问题讨论】:

    标签: android android-contacts


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2016-03-26
      • 2012-02-11
      • 2012-01-01
      • 2011-01-22
      相关资源
      最近更新 更多