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的工具类. 其实这个工具 ...
随机推荐
- Python——字符串2.0(实验)
直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改
- python课程分享2-伊嬛
2.4 模块 2.4.1 模块的概念 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,使用者可以把很多函数分组,分别放到不同 ...
- elemet-ui图标—特殊字符的unicode编码表
https://blog.csdn.net/lurr88/article/details/79754811
- Python之MySQL基础
一.存储引擎 1.1 什么是存储引擎 MySQL中的数据通过不同的技术存储再文件或者内存中,每种技术有不同的存储机制,索引技巧,锁定水平,并且提供不同的能力,而实现这些技术的我们就称之为存储引擎 1 ...
- Spark SQL UDAF示例
UDAF:用户自定义聚合函数 Scala 2.10.7,spark 2.0.0 package UDF_UDAF import java.util import org.apache.spark.Sp ...
- “浅入浅出”函数防抖(debounce)与节流(throttle)
函数防抖与节流是日常开发中经常用到的技巧,也是前端面试中的常客,但是发现自己工作一年多了,要么直接复用已有的代码或工具,要么抄袭<JS高级程序设计>书中所述"函数节流" ...
- Mac smartsvn破解及license文件
第一步:去官网下载自己系统smartsvn版本文件 下载地址:http://www.smartsvn.com/download 第二步:破解 (1) 将文件解压到系统路径:/opt/smartsvn ...
- spark DataFrame 读写和保存数据
一.读写Parquet(DataFrame) Spark SQL可以支持Parquet.JSON.Hive等数据源,并且可以通过JDBC连接外部数据源.前面的介绍中,我们已经涉及到了JSON.文本格式 ...
- linux中sogou输入法崩溃重启
经常在linux中搜狗输入法用着用着就崩溃了,无法输入中文,又不想重启电脑,照着下面在终端输入命令可以重启输入法: 1.先关闭fcitx(小企鹅输入法,提供了良好的中文输入法环境) # killall ...
- win10自带的防火墙Windows Defender
Windows Defender防火墙(别名:windows守卫者)是微软公司自主研发的一款基于windows自身保护的一款系统. Windows Defender可以对系统进行实时监控,对于Wind ...