Android 获取联系人。
PHP中文网
PHP中文网 2017-04-17 17:39:23
0
2
399

获取通讯录,一条记录里面只有一条电话号码没问题,但是如果有2条电话的话只能查询出来一条。
另外一条该怎么取?

PHP中文网
PHP中文网

认证高级PHP讲师

Antworte allen(2)
小葫芦

先把所有的号码都查询出来,然后用游标遍历

大家讲道理

看下这段代码应该可以解决
//获取通讯录学员
public void getLocalContactsInfos(Context context) {

ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
LogUtil.e("contact count  ", cursor.getColumnCount());
while (cursor.moveToNext()) {
    //取得联系人名字
    int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
    String name = cursor.getString(nameFieldColumnIndex);
    //取得电话号码
    String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);

    String PhoneNumber = "";
    //多个电话号码时遍历
    while (phone.moveToNext()) {
        PhoneNumber =  phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        //格式化手机号
        PhoneNumber = PhoneNumber.replace("-", "");
        PhoneNumber = PhoneNumber.replace(" ", "");

      
    }
}
cursor.close();
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!