public void onCreate(Bundle savedInstanceState)
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext())
{
try{
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
String phoneNumber = "";
//取得该联系人,电话数量
int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(phoneCount>0){
//获得联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId,
null, null);
if(phones.moveToFirst()){
do{
//遍历所有的电话号码
phoneNumber += phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)) + "\t";
System.out.println(phoneNumber);
}while(phones.moveToNext());
}
}
string += contact + ":" + phoneNumber +"\n";
}catch(Exception ex){
ex.printStackTrace();
}
//cursor.moveToNext();
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext())
{
try{
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
String phoneNumber = "";
//取得该联系人,电话数量
int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(phoneCount>0){
//获得联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId,
null, null);
if(phones.moveToFirst()){
do{
//遍历所有的电话号码
phoneNumber += phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)) + "\t";
System.out.println(phoneNumber);
}while(phones.moveToNext());
}
}
string += contact + ":" + phoneNumber +"\n";
}catch(Exception ex){
ex.printStackTrace();
}
//cursor.moveToNext();
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}