广告
返回顶部
首页 > 资讯 > 移动开发 >android利用ContentResolver访问者获取手机联系人信息
  • 617
分享到

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

联系手机Android 2022-06-06 04:06:46 617人浏览 八月长安
摘要

利用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;
  }
}
您可能感兴趣的文章:Android获取手机通讯录、sim卡联系人及调用拨号界面方法Android 获取手机联系人实例代码详解Android获取手机联系人信息Android获取手机联系人电话号码并返回结果Android读取手机通讯录联系人到自己项目android如何获取手机联系人的数据库示例代码Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法实例小结浅谈Android手机联系人开发之增删查改功能Android ContentProvider获取手机联系人实例Android ContentProvider实现手机联系人读取和插入


--结束END--

本文标题: android利用ContentResolver访问者获取手机联系人信息

本文链接: https://www.lsjlt.com/news/22208.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作