码迷,mamicode.com
首页 > 移动开发 > 详细

Android 选择通讯录联系人回显

时间:2018-01-30 16:28:16      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:first   att   move   启动   row   nbsp   void   close   界面   

  1.启动系统选择联系人界面

  Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);

  startActivityForResult(intent, 0);

 

  2.重写OnActivityResult方法获取数据

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
switch (requestCode)
{
  case 0:
   Uri conatctData = data.getData();
  Cursor cursor = managedQuery(conatctData, null, null, null, null);
  if (cursor.moveToFirst()) {
  String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
  String names = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)) ;
  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"="+contactId, null, null);
  String phonenum="";
if (phones.moveToFirst()) {
phonenum = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
name.setText(names);
phone.setText(phonenum);
if (Build.VERSION.SDK_INT <14) {
phones.close();
}
}
if (Build.VERSION.SDK_INT <14) {//不添加的话Android4.0以上系统运行会报错
cursor.close();
}
break;
}

}

  }

  3.添加权限

  <uses-permission android:name="android.permission.READ_CONTACTS"/>




Android 选择通讯录联系人回显

标签:first   att   move   启动   row   nbsp   void   close   界面   

原文地址:https://www.cnblogs.com/revolve/p/8384190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!