第一步:注册权限

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

第二步:接收的model类

public class ContactModel {
private String name;
private ArrayList<String > list; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public ArrayList<String> getList() {
return list;
} public void setList(ArrayList<String> list) {
this.list = list;
}
}

第三步:mian——activity

public class MainActivity extends AppCompatActivity {

    private Button button;

    private ListView listView;

    static final String TAG = "MainActivity";

    ArrayList<ContactModel>dataList = new ArrayList<ContactModel>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.contrcts_view);
readContacts(); MainAdapter adapter = new MainAdapter(this,dataList);
listView.setAdapter(adapter);
} private void readContacts(){ Cursor cursor = null; try {
cursor = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null); int contactIdIndex = 0;
int nameIndex = 0; if(cursor.getCount() > 0) {
contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
} ContactModel model = null; while(cursor.moveToNext()) { model = new ContactModel(); String contactId = cursor.getString(contactIdIndex);
String name = cursor.getString(nameIndex);
// Log.i(TAG, contactId);
Log.i(TAG, "==============:" + name); model.setName(name); /*
* 查找该联系人的phone信息
*/
Cursor phones = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null, null);
int phoneIndex = 0;
if (phones.getCount() > 0) {
phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
}
ArrayList <String>list = new ArrayList<String>();
while (phones.moveToNext()) { String phoneNumber = phones.getString(phoneIndex);
Log.i(TAG, "==============:" + phoneNumber);
list.add(phoneNumber);
} model.setList(list); dataList.add(model);
} }catch (Exception e){
Log.e(TAG,e.toString());
}finally {
if(cursor != null){
cursor.close();
}
} } }

第四步:adapter

public class MainAdapter extends BaseAdapter {

    private Context context;
private List<ContactModel> list; public MainAdapter(Context context, List<ContactModel>list){
this.context = context;
this.list = list;
} @Override
public int getCount() {
return this.list.size();
} @Override
public Object getItem(int i) {
return this.list.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder;
if(view == null){
view = LayoutInflater.from(this.context).inflate(R.layout.item_main,null);
holder = new ViewHolder();
holder.nameTV = (TextView)view.findViewById(R.id.name);
holder.phoneTV = (TextView)view.findViewById(R.id.phone);
view.setTag(holder);
}else{
holder = (ViewHolder)view.getTag();
} ContactModel model = this.list.get(i);
holder.nameTV.setText(model.getName()); StringBuffer buffer = new StringBuffer(); for (int j=0;j<model.getList().size();j++){
if(j==0){
buffer.append(model.getList().get(j));
}else {
buffer.append("\n"+model.getList().get(j));
}
}
holder.phoneTV.setText(buffer.toString()); return view;
} class ViewHolder{
TextView nameTV;
TextView phoneTV;
}
}

第五步:item 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tom"
android:layout_margin="5dp"
android:id="@+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="18580633453"
android:layout_margin="5dp"
android:id="@+id/phone"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d6d6d6"
/> </LinearLayout>

main的布局就一个listview,如果不会的话我需要静静了

2.系统通讯录这里是使用fragement做的,要用startActivityForResult

   textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
}); @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { // ContentProvider展示数据类似一个单个数据库表
// ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
ContentResolver reContentResolverol = getActivity(). getContentResolver();
// URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
Uri contactData = data.getData();
// 查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
Cursor cursor = getActivity().managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
// 获得DATA表中的名字
username = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // 条件为联系人ID
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获得DATA表中的电话号码,条件为联系人ID,因为手机号码可能会有多个
Cursor phone = reContentResolverol.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null); while (phone.moveToNext()) {
usernumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// et_mobile.setText(usernumber + " (" + username + ")");
toastShowShort(usernumber + " (" + username + ")");
} }
}

android读取通讯录和使用系统通讯录的更多相关文章

  1. Android项目--获取系统通讯录列表

    ----------------- 通讯录列表 ----------------- 按常理来说,获取系统通讯录列表,无非就是将通讯录的数据库打开获取数据,适配,添加即可. Cursor cursor; ...

  2. Android项目--浅析系统通讯录中的那些方法

    系统通讯录,以前的版本虽然过时了,不过有些东西还是可以用. 1.开启系统联系人添加 /** 添加联系人 */ Intent intent = new Intent(Intent.ACTION_INSE ...

  3. ios(包括6、7)应用程序引用系统通讯录的方法 [亲测可行]

    由于ios系统对用户隐私的控制,第三方应用程序只能通过苹果官方接口调用系统通讯录,不能像android那样直接操作通讯录数据库.     一般地,使用系统自带通讯录的方法有两种,一种是直接将整个通讯录 ...

  4. iOS调用系统通讯录获取姓名电话号码(转)

    原文地址:http://blog.csdn.net/idoshi201109/article/details/46007125 OS调用系统通讯录获取姓名电话号码 (iOS 8.0 Xcode6.3可 ...

  5. iOS开发--系统通讯录的访问与添加联系人

    公司项目有访问通讯录的需求,所以开始了探索之路.从开始的一无所知,到知识的渐渐清晰.这一切要感谢广大无私分享的 “coder”,注:我是尊称的语气! 苹果提供了访问系统通讯录的框架,以便开发者对系统通 ...

  6. iOS:ABPeoplePickerNavigationController系统通讯录使用

    昨天因项目需求要访问系统通讯录获取电话号码,于是乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了.写这边博客是为了记录一下,自己下一次弄的时候就别在出错了.同时,有和我一样的菜鸟能够避免走一下弯路. ...

  7. Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框

    作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]h ...

  8. IOS 获取系统通讯录

    进入正题  获取系统通讯录,不想多讲,留下链接http://my.oschina.net/joanfen/blog/140146 通常做法: 首先创建一个ABAddressBookRef类的对象add ...

  9. iOS开发之系统通讯录

                          @iOS调用操作通讯录所用的库文件                                         AddressBook.framewor ...

随机推荐

  1. 监听程序未启动或数据库服务未注册到该监听程序。启动该监听程序并注册数据库服务 然后重新运行 em configuration assistant。

    在WIN 7/64Bit上安装ORACLE 11gR2后,管理网页Database Control(如:https://localhost:1158/em)始终登录不进去,总是说密码错误,使用配置工具 ...

  2. ADO连接数据库【msado15.dll】

    Microsoft ActiveX Data Objects (ADO) 注册表查看ADO版本:HKEY_LOCAL_MACHINE\Software\Microsoft\DataAccess下有Ve ...

  3. [转]ASP.NET MVC 5– 使用Wijmo MVC 5模板1分钟创建应用

    开始使用 使用ComponentOne Studio for ASP.NET Wijmo制作MVC5应用程序,首先要做的是安装Studio for ASP.NET Wijmo . 测试环境 VS201 ...

  4. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from

    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from ...

  5. 【RF库Collections测试】Get From List

    Name:Get From ListSource:Collections <test library>Arguments:[ list_ | index ]Returns the valu ...

  6. Python 入门(九)迭代

    什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...

  7. 管理开机启动:chkconfig

    CentOS 6 如何设置服务开机启动: [root@localhost ~]$ ls /etc/init.d/httpd # /etc/init.d/目录下必须有启动脚本 [root@localho ...

  8. Triton调试记录

    先编译Release版本 先从下拉列表选择Release-MT-DLL,然后选中Triton-vc14工程, 修改项目属性配置为Release-MT-DLL-NODX,NODX的意思是不使用Direc ...

  9. 【PHP+Redis】 php-redis 操作类 封装

    <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...

  10. C/C++程序编译流程

    单个文件的编译过程 多个文件的编译过程