转载自:http://www.jb51.net/article/106379.htm

首先需要在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利用ContentResolver访问者获取手机联系人信息的更多相关文章

  1. Expo大作战(三十九)--expo sdk api之 DocumentPicker,Contacts(获取手机联系人信息),Branch

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  2. Android软件开发之获取通讯录联系人信息

    Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来. 这一篇文章我主要带领同学们熟悉Android的通讯录机制. 图中选中 ...

  3. Android 获取手机联系人信息

    //获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Conten ...

  4. Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息

    Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息 本文目录: 获取手机信息 设置权限 申请权限 查询联系人 获取手机定位信息 调用高德地图,设置显示2个坐标点的位置,以及 ...

  5. 【转】android 安卓APP获取手机设备信息和手机号码的代码示例

    http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...

  6. android 安卓APP获取手机设备信息和手机号码的代码示例

    下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...

  7. android -------- 获取手机设备信息

    最近在开发中,需要用到一些系统信息,总结了一下 /** * Created by zhangqie on 2019/2/26 * Describe: 系统工具类 */ public class Equ ...

  8. Android-AsyncTask异步任务(获取手机联系人)

    本篇随笔将讲解一下Android的多线程的知识,以及如何通过AsyncTask机制来实现线程之间的通信. 一.Android当中的多线程 在Android当中,当一个应用程序的组件启动的时候,并且没有 ...

  9. AppUtils【获取手机的信息和应用版本号、安装apk】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 一个获取设备的系统版本号.设备的型号.应用版本号code值.应用版本号name值.包名.是否更新.安装apk的工具类. 其实这个工具 ...

随机推荐

  1. Beautiful用法总结

    一.安装 通过命令:pip3 install Beautifulsoup4: 安装后运行:from bs4 import BeautifulSoup,没有报错,说明安装正常: 二.解析库 Beauti ...

  2. C++简单交换堆排序的代码

    下面的内容内容是关于C++简单交换堆排序的内容,应该对各位朋友有较大用途. { int start, end; { }} { int root, child; { if((child + 1 < ...

  3. md5加密 bas64加密

    /** * 获取bas64加密的算法 * @param str * @return */ public static String getBase64(String str) { byte[] b = ...

  4. 用spark-submit启动程序

    来源:http://spark.apache.org/docs/latest/submitting-applications.html 提交程序常用的一些选项 ./bin/spark-submit \ ...

  5. 三大家族(offset、scroll、client)

    offset.scroll.client三大家族 offset家族 offsetWidth 与 offsetHeight offset 偏移 用于获取元素自身的位置和大小 offsetWidth和of ...

  6. Tomcat映射路径

    打开tomcat安装包,在config目录下修改server.xml文件: 在<Host>标签中添加: <Context path="" docBase=&quo ...

  7. hdu6489 2018 黑龙江省大学生程序设计竞赛j题

    Problem Description Kayaking is playing a puzzle game containing n different blocks. He marks the bl ...

  8. BandwagonHost

    https://kiwivm.64clouds.com/main-exec.php?mode=extras_shadowsocks https://kiwivm.64clouds.com/main-e ...

  9. <玩转Django2.0>读书笔记:URL规则和视图

    1. 带变量的URL #urls.py from django.urls import path from .view import * urlpatterns = [ path('',index_v ...

  10. 使用HttpClient发送文件流到服务器端

    适用场景:网络绝对路径的URL文件或图片,不存储到本地,转换成stream,直接使用HTTPClient传送到SpringBoot的服务端,将文件存储下来,并返回一个文件地址.目前分层架构的系统越来越 ...