1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonQuary);
name = (TextView) findViewById(R.id.tv_name);
phOne= (TextView) findViewById(R.id.tv);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://contacts/people");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
if (data == null) {
return;
}
//处理返回的data,获取选择的联系人信息
Uri uri = data.getData();
getPhoneContacts(uri);
name.setText(myname);
phone.setText(phonenumber);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private String[] getPhoneContacts(Uri uri) {
String[] cOntact= new String[2];
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(uri, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
//取得联系人姓名
int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
myname = cursor.getString(nameFieldColumnIndex);
/*//取得电话号码
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);
if (phone != null) {
phone.moveToFirst();
phOnenumber= phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phone.close();*/
cursor.close();
} else {
return null;
}
return contact;
} |