项目中经常需要选择联系人的功能,如果没有特殊的邀请的话,系统自带的联系人界面就已经够用了:
测试平台:android 4.2
显示效果:
系统自带界面调用代码:
1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 2 intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 3 startActivityForResult(intent, 1);
要得到选择的联系人信息,需要复写onActivityResult
1 @Override 2 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 3 super.onActivityResult(requestCode, resultCode, data); 4 if (data != null) { 5 Uri uri = data.getData(); 6 if (uri != null) { 7 Cursor cursor = getContentResolver() 8 .query(uri, 9 new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, 10 null, null, null); 11 while (cursor.moveToNext()) { 12 String number = cursor.getString(0); 13 String name = cursor.getString(1); 14 } 15 16 } 17 } 18 19 }