android利用ContentResolver访问者获取手机联系人信息
转载自: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访问者获取手机联系人信息的更多相关文章
- Expo大作战(三十九)--expo sdk api之 DocumentPicker,Contacts(获取手机联系人信息),Branch
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- Android软件开发之获取通讯录联系人信息
Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来. 这一篇文章我主要带领同学们熟悉Android的通讯录机制. 图中选中 ...
- Android 获取手机联系人信息
//获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Conten ...
- Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息
Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息 本文目录: 获取手机信息 设置权限 申请权限 查询联系人 获取手机定位信息 调用高德地图,设置显示2个坐标点的位置,以及 ...
- 【转】android 安卓APP获取手机设备信息和手机号码的代码示例
http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...
- android 安卓APP获取手机设备信息和手机号码的代码示例
下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...
- android -------- 获取手机设备信息
最近在开发中,需要用到一些系统信息,总结了一下 /** * Created by zhangqie on 2019/2/26 * Describe: 系统工具类 */ public class Equ ...
- Android-AsyncTask异步任务(获取手机联系人)
本篇随笔将讲解一下Android的多线程的知识,以及如何通过AsyncTask机制来实现线程之间的通信. 一.Android当中的多线程 在Android当中,当一个应用程序的组件启动的时候,并且没有 ...
- AppUtils【获取手机的信息和应用版本号、安装apk】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 一个获取设备的系统版本号.设备的型号.应用版本号code值.应用版本号name值.包名.是否更新.安装apk的工具类. 其实这个工具 ...
随机推荐
- 移动端右侧导航 显示隐藏js
transform与fixed影响 html按钮 <span class="nav-btn"></span> <span class="cl ...
- js高级类型
一.funciton数据类型 1.定义:一个function类型对象,用于管理一个具体函数. function类型相当于Java中java.lang.reflect.Method 2.函数类型对象创建 ...
- python-装饰器&生成器&迭代器&推导式
一:普通装饰器 概念:在不改变原函数内部代码的基础上,在函数执行之前和之后自动执行某个功能,为已存在的对象添加某个功能 普通装饰器编写的格式 def 外层函数(参数) def 内层函数(*args,* ...
- [原创]基于Zybo SDIO WiFi模块调试
采用的是RTL8189 SDIO 模块,介绍如下 The Realtek RTL8189ES-VB-CG is a highly integrated single-chip 802.11n Wire ...
- .net core Swagger 过滤部分Api
因为场景需要,要把某些特定的api过滤掉,不允许显示在swaggerui里, 具体操作步骤: 分为三步 步骤1: 创建Attribute /// <summary> /// igno ...
- 如何把遗留的Java应用托管在Service Fabric中
一.概述 众所周知,微服务化尤其对遗留系统进行微服务化一般采用"Lift and Shift"的模式进行. Service Fabric作为一个微服务托管平台,不仅仅可以在上面跑. ...
- nginx配置负载均衡
本教程不讲解nginx的安装,若安装请看博客 http://www.cnblogs.com/hqjy/p/8092983.html 本教程不讲解tomcat的安装,若安装请看博客 http://www ...
- 咸鱼入门到放弃10--javaweb的两种开发模式
(本篇是之前方法的综合使用,新东西不多,其中也涉及三层架构的问题.此处直接引用了大佬blog:https://www.cnblogs.com/xdp-gacl/p/3908610.html) SUN公 ...
- Alpha冲刺——代码规范、冲刺任务与计划
代码规范 作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 ...
- kvm虚拟机存储管理
一.kvm存储虚拟化介绍: 1.KVM 的存储虚拟化是通过存储池(Storage Pool)和卷Volume)来管理的. 2.Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种型 ...