其query 方法查询出符合标准的电话本记录,最后将这些数据都显示在一个
TextView 中即可。
public class Activity01 extends Activity
{
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()){
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
String number = cursor.getString(numberFieldColumnIndex);
string += (contact+":"+number+"\n");
}
cursor.close();
//设置TextView 显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}
<uses-permission
android:name="android.permission.READ_CONTACTS">
</uses-permission>