获取联系人名称及全部电话的实例
CODE:
String[] projection = new String[]{
People._ID,
People.NAME
};
Uri peopleUri = People.CONTENT_URI;
Log.d( TAG, "peopleUri: " + peopleUri );
Cursor peopleCursor = managedQuery( peopleUri, projection, null, null, People.DEFAULT_SORT_ORDER );
if( peopleCursor == null ){
Log.e( TAG, "peopleCursor is null");
return false;
}
if( peopleCursor.moveToFirst()){
boolean isAfterLastPeople = peopleCursor.isAfterLast();
while( !isAfterLastPeople ){
String name = peopleCursor.getString(peopleCursor.getColumnIndexOrThrow( People.NAME ));
Log.d( TAG, "name: " + name );
Long personId = peopleCursor.getLong( peopleCursor.getColumnIndexOrThrow(People._ID) );
Uri personUri = ContentUris.withAppendedId(peopleUri, personId );
Uri phoneUri = Uri.withAppendedPath( personUri, People.Phones.CONTENT_DIRECTORY );
Log.d( TAG, "phoneUri: " + phoneUri );
Cursor phoneCursor = managedQuery( phoneUri, null, null, null, People.Phones.DEFAULT_SORT_ORDER );
if( phoneCursor == null ){
Log.e( TAG, "phoneCursor is null");
return false;
}
if( phoneCursor.moveToFirst()){
boolean isAfterLastPhone = phoneCursor.isAfterLast();
while( !isAfterLastPhone ){
String number = phoneCursor.getString( phoneCursor.getColumnIndexOrThrow( PhonesColumns.NUMBER));
String type = phoneCursor.getString( phoneCursor.getColumnIndexOrThrow( PhonesColumns.TYPE ));
Log.d( TAG, "number:" + number );
Log.d( TAG, "type:" + type );
phoneCursor.moveToNext();
isAfterLastPhone = phoneCursor.isAfterLast();
}
}
peopleCursor.moveToNext();
isAfterLastPeople = peopleCursor.isAfterLast();
}