android利用ContentResolver访问者获取手机联系人信息
程序员文章站
2023-12-16 23:07:34
利用contentresolver内容访问者,获取手机联系人信息我做了两种不同的做法。第一种,直接获取所有手机联系人信息,展示在listview中。第二种,通过butten...
利用contentresolver内容访问者,获取手机联系人信息我做了两种不同的做法。第一种,直接获取所有手机联系人信息,展示在listview中。第二种,通过butten按钮跳转到系统的手机联系人界面,单个获取手机联系人信息,展示在listview中,结果如下:
第一种:
第二种:
第一种:直接获取所有手机联系人信息
首先需要在androidmanifest.xml文件中添加权限:
<uses-permission android:name="android.permission.read_contacts" /> activity_main.xml布局: <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.mainactivity"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_lxr" > </listview> </linearlayout>
activity_xs.xml布局:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_xs" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.xsactivity"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_name" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_telephone" /> </linearlayout>
mainactivity类:
private listview lv_lxr; private button b_name; private contentresolver cr; private list<map<string, object>> datalistview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //获得listview lv_lxr = (listview) findviewbyid(r.id.lv_lxr); //得到访问者 cr = getcontentresolver(); //定义一个接收联系人姓名和电话号码的集合 datalistview = new arraylist<>(); uri uri=uri.parse("content://com.android.contacts/raw_contacts"); cursor cursor= cr.query(uri,null,null,null,null); while(cursor.movetonext()){ int id=cursor.getint(cursor.getcolumnindex("_id")); uri uridata=uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"); cursor contactdata= cr.query(uridata,null,null,null,null); //用来装姓名 string aa=""; //用来装号码 string bb=""; while(contactdata.movetonext()){ string type=contactdata.getstring(contactdata.getcolumnindex("mimetype")); //如果获取的是vnd.android.cursor.item/phone_v2则是号码 if(type.equals("vnd.android.cursor.item/phone_v2")){ bb=contactdata.getstring(contactdata.getcolumnindex("data1")); //如果获取的是vnd.android.cursor.item/name则是姓名 }else if(type.equals("vnd.android.cursor.item/name")) { aa=contactdata.getstring(contactdata.getcolumnindex("data1")); } } //将用户名和号码放入map集合中 map<string,object> map=new hashmap<>(); map.put("images",aa); map.put("titles",bb); datalistview.add(map); } simpleadapter adapter=new simpleadapter(this, datalistview,r.layout.activity_xs,new string[]{"images","titles"},new int[]{r.id.tv_name,r.id.tv_telephone}); lv_lxr.setadapter(adapter); }
第二种:通过butten按钮跳转到系统的手机联系人界面,单个获取手机联系人信息,展示在listview中
activity_contacts.xml布局:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_contacts" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.contactsactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到联系人页面" android:id="@+id/b_tzcontacts" /> </linearlayout> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_contacts" ></listview> </linearlayout>
contactsactivity类:
private button b_tzcontacts; private string phonename; private string phonenumber; private list<map<string,object>> datalistview; private listview lv_contacts; private simpleadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_contacts); //获得跳转到联系人的id b_tzcontacts =(button) findviewbyid(r.id.b_tzcontacts); //获得listview的id lv_contacts =(listview) findviewbyid(r.id.lv_contacts); //定义一个接受联系人姓名和电话号码的集合 datalistview = new arraylist<>(); //获取联系人的点击事件 b_tzcontacts.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intentphone=new intent(intent.action_pick); intentphone.setdata(contactscontract.contacts.content_uri); startactivityforresult(intentphone,0); } }); //r.layout.activity_xs就是上文的activity_xs布局问价 adapter = new simpleadapter(this, datalistview, r.layout.activity_xs,new string[]{"images","titles"},new int[]{r.id.tv_name,r.id.tv_telephone}); lv_contacts.setadapter(adapter); } //获得返回的结果 @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch (requestcode){ case 0: if(resultcode== activity.result_ok){ uri uri=data.getdata(); cursor cursor=managedquery(uri,null,null,null,null); cursor.movetofirst(); string contactid=cursor.getstring(cursor.getcolumnindex(contactscontract.contacts._id)); //得到contentresolver contentresolver contentresolver=getcontentresolver(); cursor phone=contentresolver.query(contactscontract.commondatakinds.phone.content_uri,null,contactscontract.commondatakinds.phone.contact_id+"="+contactid,null,null); while (phone.movetonext()){ //联系人 phonename =phone.getstring(phone.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); //手机号码 phonenumber =phone.getstring(phone.getcolumnindex(contactscontract.commondatakinds.phone.number)); //格式化手机号 phonenumber = phonenumber.replace("-",""); phonenumber = phonenumber.replace("",""); //将用户名和号码放入map集合中 map<string,object> map=new hashmap<>(); map.put("images",phonename); map.put("titles",phonenumber); datalistview.add(map); } //刷新适配器 adapter.notifydatasetchanged(); } break; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。