不同版本SDK电话本内容的获取(电话,姓名等)

低版本:

private void getContactsInLowVersion() {


        Cursor tempContactsCursor = ContactActivity
                .this.getContentResolver().query(Contacts.People.CONTENT_URI, null,
                        null, null, null);
        System.out.println(tempContactsCursor.getCount() + "");
        if (tempContactsCursor.moveToFirst()) {
            do {
                int peopleNameIndex = tempContactsCursor.getColumnIndex(Contacts.People.DISPLAY_NAME);
                int mobileNumberIndex = tempContactsCursor.getColumnIndex(Contacts.Phones.NUMBER);
                String name = tempContactsCursor.getString(peopleNameIndex);
                String num = tempContactsCursor.getString(mobileNumberIndex);
                contactNameList.add(name + "");
            contactNumList.add(num + "");
            } while (tempContactsCursor.moveToNext());
            tempContactsCursor.close();
        }
    }


高版本:

private void getContactsInHighVersion() {
Cursor cur = getContentResolver().query(  
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);  
        startManagingCursor(cur);  
        int num = cur.getCount();
        System.out.println(num + "");
        int count = 0;
        while (cur.moveToNext()) {  
        count ++;
   
            long id = cur.getLong(cur.getColumnIndex("_id"));  
            Cursor pcur = getContentResolver().query(  
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
                    null,  
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="  
                            + Long.toString(id), null, null);  
   
            // 处理多个号码的情况  
            String phoneNumbers = "";  
            while (pcur.moveToNext()) {  
                String strPhoneNumber = pcur  
                        .getString(pcur  
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
                phoneNumbers += strPhoneNumber + ":";  
            }  
            phoneNumbers += "\n";  
            pcur.close();
            String name = cur.getString(cur.getColumnIndex("display_name"));
            contactNameList.add(name);
            contactNumList.add(phoneNumbers);
        }  
        cur.close();
}

你可能感兴趣的:(不同版本SDK电话本内容的获取(电话,姓名等))