- String string = "";
- super.onCreate(savedInstanceState);
- ContentResolver cr = getContentResolver();
- Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
- while (cursor.moveToNext())
- {
- int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
- String contact = cursor.getString(nameFieldColumnIndex);
- string += contact + ":";
- 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);
- while (phone.moveToNext())
- {
- int numberFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
- String strPhoneNumber = phone.getString(numberFieldColumnIndex);
- string += " " + strPhoneNumber;
- }
- string += "\n";
- phone.close();
- }
- cursor.close();
可见,在2.0之后的版本中要获取手机号码就只能通过先取得联系人ID然后再取得联系人电话号码, 而且号码可能不止一个.