android获取手机通讯录

news/2024/7/7 18:42:46

在android中读取联系人信息的程序,包括读取联系人姓名、手机号码和邮箱

(转载自博客:http://www.cnblogs.com/error404/archive/2013/03/12/2956090.html)

 1:androidmanifest.xml的内容

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="cn.itcast.contacts"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><uses-library android:name="android.test.runner" /><activity android:name=".MainActivity"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><uses-sdk android:minSdkVersion="8" /><uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" /><instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="cn.itcast.contacts" android:label="Tests for My App" />
</manifest> 
复制代码

里面重要的是搭建测试环境和添加<uses-permission>,即读取和写入联系人信息的权限

2:读取的主要方法

复制代码
/** 读取联系人的信息*/public void testReadAllContacts() {Cursor cursor = this.getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);int contactIdIndex = 0;int nameIndex = 0;if(cursor.getCount() > 0) {contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);}while(cursor.moveToNext()) {String contactId = cursor.getString(contactIdIndex);String name = cursor.getString(nameIndex);Log.i(TAG, contactId);Log.i(TAG, name);/** 查找该联系人的phone信息*/Cursor phones = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);int phoneIndex = 0;if(phones.getCount() > 0) {phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);}while(phones.moveToNext()) {String phoneNumber = phones.getString(phoneIndex);Log.i(TAG, phoneNumber);}/** 查找该联系人的email信息*/Cursor emails = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null);int emailIndex = 0;if(emails.getCount() > 0) {emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);}while(emails.moveToNext()) {String email = emails.getString(emailIndex);Log.i(TAG, email);}}}
复制代码

 

3:目前手机中的联系人信息,有两个联系人的信息,如图所示,

这是Hellen的联系信息

这是Mike的联系信息

 

4:测试结果,在控制台输出的内容为:

可以看到在控制台输出了两个联系人的信息。

 

//--------------------------------------

获取所有分组编号和组的名称

复制代码
Cursor groupInfoCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, null);
while(groupInfoCursor.moveToNext()) {int idIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups._ID);int titleIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups.TITLE);String id = groupInfoCursor.getString(idIndex);String title = groupInfoCursor.getString(titleIndex);
}
groupInfoCursor.close();

转载于:https://www.cnblogs.com/new-comer/p/5498459.html


http://lihuaxi.xjx100.cn/news/244516.html

相关文章

ElasticSearch5.6安装head插件

1.先要下载插件&#xff0c;但是网上下载地址git根本下载不了&#xff0c;最后从一个博客的百度网盘下的 https://pan.baidu.com/s/1M0rkdVRLf2sIpVYdrRbxtg 2.安装过程 需要先安装node环境 然后切换到head安装目录 运行npm install 我在运行这个命令的时候报错 解决办法&…

asp.net input怎么获取值

前台&#xff1a; <input type"hidden" name"content" value"content"> 后台&#xff1a; Request.Form["content"].ToString(); 切记&#xff1a;name不能缺少&#xff0c;id可由可无。>如有问题&#xff0c;请联系我&…

ubuntu下安装ftp服务器

ubuntu下安装ftp服务器 Ftp服务器是使用以vsftp为例。1. 安装 $sudo aptitude install vsftpd $ps aux | grep ftp2. 配置文件 /etc/vsftpd.conf 文件中都包含配置选项&#xff0c;要使选项起作用&#xff0c;可去掉注释符&#xff03;3. 重启服务器 /etc/init.…

SpringBoot整合Elasticsearch

1.下载es软件 2.在application.properties #elasticsearch配置 #该名称需要和安装的es配置文件里面的保持一致 spring.data.elasticsearch.cluster-namemy-application spring.data.elasticsearch.cluster-nodes127.0.0.1:9300 #连接超时的时间 spring.data.elasticsearch.pr…

html5知识点补充—hgroup元素的使用

使用hgroup元素组合标题 使用新的HTML5元素hgroup&#xff0c;可以为header元素添加更多的信息。 这个元素用来对多个相关联的h1~h6标题进行分组。如果你的网站有副标题&#xff0c;可以使用hgroup元素。虽然hgroup是一个有效的分组选项&#xff0c;但是它主要是用来告知文档大…

python基础04

python基础04 python2在编译安装时&#xff0c;可以通过参数 -----enable----unicodeucs2 或 -----enable--unicodeucs4 分别用于指定使用2个字节&#xff0c;4个字节表示一个Unicode字符。python3无法进行选择&#xff0c;默认使用usc4. 查看当前python中表示Unicode字符串时占…

某内存池中的指针用法

内存池实现有许多种&#xff0c;各有不同的优缺点。 这里不是主要说内存池&#xff0c;只是觉得这个内存池中的指针用得很飘逸&#xff01; template <class T,int AllocSize 50> class MemPool { public: static void* operator new(size_t allocLength) …

git常用命令的使用

个人资源与分享网站&#xff1a;http://xiaocaoshare.com/ 1.拉取资源库代码 git clone 资源库地址 2.查看当前分支状态 git status 3.更新代码 git pull 4.git还原某个文件 git checkout -- src/main/java/cn/wkgb/common/aes/Test4.java 5.提交代码 6.撤销提交…