public class MainActivity extends AppCompatActivity { PRivate ContentResolver contentResolver; private ListView contacts_list; private Cursor cursor1; private List<Map<String,String>> list=new ArrayList<>(); private SimpleAdapter simpleAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 控件 initview(); } //初始化 控件 private void initview(){ //获取内容 访问者 contentResolver = getContentResolver(); contacts_list = (ListView) findViewById(R.id.contacts_list); //获得所有 联系人 findViewById(R.id.getAllContacts_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Uri uri=Uri.parse("content://com.android.contacts/raw_contacts"); Cursor cursor=contentResolver.query(uri,null,null,null,null); while (cursor.moveToNext()){ //获得所有联系人的 id int id=cursor.getInt(cursor.getColumnIndex("_id")); String name=cursor.getString(cursor.getColumnIndex("display_name")); //继续获取相对应的联系人的数据(电话号码) Uri uriAll=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"); cursor1 = contentResolver.query(uriAll,null,null,null,null); //获取联系人 id 名字 电话 while (cursor1.moveToNext()){ String data1=cursor1.getString(cursor1.getColumnIndex("data1")); String type=cursor1.getString(cursor1.getColumnIndex("mimetype")); if("vnd.android.cursor.item/phone_v2".equals(type)){ Map<String,String> map=new HashMap<String, String>(); map.put("id",id+""); map.put("name",name); map.put("phone",data1); list.add(map); } } simpleAdapter = new SimpleAdapter(MainActivity.this,list, R.layout.contacts_item,new String[]{"id","name","phone"},new int[]{R.id.contacts_id_tv,R.id.contacts_name_tv,R.id.contacts_phone_tv}); contacts_list.setAdapter(simpleAdapter); } } }); //页面跳转 获取指定联系人 findViewById(R.id.ItetentJumpForSingleContacts_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); MainActivity.this.startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: if (resultCode == RESULT_OK) { Uri contactData = data.getData(); Cursor cursor = managedQuery(contactData, null, null, null, null); cursor.moveToFirst(); String num = this.getContactPhone(cursor); Toast.makeText(this, ""+num, Toast.LENGTH_SHORT).show(); } break; default: break; } } private String getContactPhone(Cursor cursor) { // TODO Auto-generated method stub int phoneColumn = cursor .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); int phoneNum = cursor.getInt(phoneColumn); String result = ""; if (phoneNum > 0) { // 获得联系人的ID号 int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID); String contactId = cursor.getString(idColumn); // 获得联系人电话的cursor Cursor phone = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null); if (phone.moveToFirst()) { for (; !phone.isAfterLast(); phone.moveToNext()) { int index = phone .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int typeindex = phone .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); int phone_type = phone.getInt(typeindex); String phoneNumber = phone.getString(index); result = phoneNumber; } if (!phone.isClosed()) { phone.close(); } } } return result; }}手动展示:
首先获取系统访问者ContnetResolver,
再通过Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");获得手机中所有的联系人Id,以及名字,
再使用 Uri uriAll=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");根据id查询该人的电话,
跳转系统界面,吐司电话号码:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); MainActivity.this.startActivityForResult(intent, 1);跳转到系统的联系人选择界面,只能单选;通过回调方法传回来的值,判断有无选中;有选中获得号码,吐司;无则无操作;
新闻热点
疑难解答