android利用ContentResolver访问者获取手机联系人信息

android利用ContentResolver访问者获取手机联系人信息,第1张

概述利用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;@OverrIDeprotected 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);      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);        //用来装姓名        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;@OverrIDeprotected 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,R.ID.tv_telephone});  lv_contacts.setAdapter(adapter);}  //获得返回的结果@OverrIDeprotected 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);        cursor.movetoFirst();        String contactID=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));        //得到ContentResolver        ContentResolver contentResolver=getContentResolver();        Cursor phone=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactID,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;  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android利用ContentResolver访问者获取手机联系人信息全部内容,希望文章能够帮你解决android利用ContentResolver访问者获取手机联系人信息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146861.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存