本篇开始讲如何从Android中得到本机联系人的信息。
由于Android较快的版本升级,部分API已经发生了变化。本篇探究的通过ContentProvider机制获取联系人的API从Android2.0开始做了很大调整,原来的android.provider.Contacts类及其下相关类由android.provider.ContactsContract代替。原类体系标记为Deprecated(废弃),因为兼容的原因目前还存在,但不保证以后的更新版本中完全丢弃。

所以本文先从Android2.1以上平台的联系人读取开始说起,下面给出代码在Android2.1/2.2中运行的效果图,

<ignore_js_op>

首先,创建类ViewContacts继承ListActivity,并设为为应用的开始Activity。
ViewContacts.java 代码:

  1. package jtapp.contacts;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import android.app.ListActivity;
  6. import android.database.Cursor;
  7. import android.os.Bundle;
  8. import android.provider.ContactsContract;
  9. import android.widget.SimpleAdapter;
  10. public class ViewContacts extends ListActivity {
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. List<HashMap<String, String>> items = fillMaps();
  17. SimpleAdapter adapter = new SimpleAdapter(
  18. this,items,R.layout.list_item,
  19. new String[]{"name","key"},
  20. new int[]{R.id.item,R.id.item2});
  21. this.setListAdapter(adapter);
  22. }
  23. private List<HashMap<String, String>> fillMaps() {
  24. List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
  25. Cursor cur = null;
  26. try {
  27. // Query using ContentResolver.query or Activity.managedQuery
  28. cur = getContentResolver().query(
  29. ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  30. if (cur.moveToFirst()) {
  31. int idColumn = cur.getColumnIndex(
  32. ContactsContract.Contacts._ID);
  33. int displayNameColumn = cur.getColumnIndex(
  34. ContactsContract.Contacts.DISPLAY_NAME);
  35. // Iterate all users
  36. do {
  37. String contactId;
  38. String displayName;
  39. String phoneNumber = "";
  40. // Get the field values
  41. contactId = cur.getString(idColumn);
  42. displayName = cur.getString(displayNameColumn);
  43. // Get number of user's phoneNumbers
  44. int numberCount = cur.getInt(cur.getColumnIndex(
  45. ContactsContract.Contacts.HAS_PHONE_NUMBER));
  46. if (numberCount>0) {
  47. Cursor phones = getContentResolver().query(
  48. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  49. null,
  50. ContactsContract.CommonDataKinds.Phone.CONTACT_ID
  51. + " = " + contactId
  52. /*+ " and " + ContactsContract.CommonDataKinds.Phone.TYPE
  53. + "=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE*/,
  54. null, null);
  55. if (phones.moveToFirst()) {
  56. int numberColumn = phones.getColumnIndex(
  57. ContactsContract.CommonDataKinds.Phone.NUMBER);
  58. // Iterate all numbers
  59. do {
  60. phoneNumber += phones.getString(numberColumn) + ",";
  61. } while (phones.moveToNext());
  62. }
  63. }
  64. // Add values to items
  65. HashMap<String, String> i = new HashMap<String, String>();
  66. i.put("name", displayName);
  67. i.put("key", phoneNumber);
  68. items.add(i);
  69. } while (cur.moveToNext());
  70. } else {
  71. HashMap<String, String> i = new HashMap<String, String>();
  72. i.put("name", "Your Phone");
  73. i.put("key", "Have No Contacts.");
  74. items.add(i);
  75. }
  76. } finally {
  77. if (cur != null)
  78. cur.close();
  79. }
  80. return items;
  81. }
  82. }

复制代码

main.xml 代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="show your phone's contacts:"
  11. />
  12. <ListView android:id="@id/android:list"
  13. android:layout_width="fill_parent"
  14. android:layout_height="0dip"
  15. android:layout_weight="1"
  16. android:drawSelectorOnTop="false"
  17. />
  18. </LinearLayout>

复制代码

list_item.xml 代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TableRow android:id="@+id/TableRow01"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. <TextView android:id="@+id/item"
  11. xmlns:android="http://schemas.android.com/apk/res/android"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:textSize="20px" />
  15. <TextView android:text=": "
  16. xmlns:android="http://schemas.android.com/apk/res/android"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:textSize="20px" />
  20. <TextView android:id="@+id/item2"
  21. xmlns:android="http://schemas.android.com/apk/res/android"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:textSize="20px" />
  25. </TableRow>
  26. </LinearLayout>

复制代码

AndroidManifest.xml 增加uses权限READ_CONTACTS 代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="jtapp.contacts" android:versionCode="1" android:versionName="1.0">
  4. <application android:icon="@drawable/icon" android:label="@string/app_name">
  5. <activity android:name=".ViewContacts" android:label="@string/app_name">
  6. <intent-filter>
  7. <action android:name="android.intent.action.MAIN" />
  8. <category android:name="android.intent.category.LAUNCHER" />
  9. </intent-filter>
  10. </activity>
  11. </application>
  12. <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
  13. </manifest>

复制代码

以上文件编写好后,应用就能够在Android2.1模拟器上正确运行了。

那么该app如果在android1.6上运行,会怎么样呢?1.6上并没有ContactsContract类体系,所以就会报错了。需要注意,ContactContract类是在API Level 5增加的,之前的Android版本并不支持。
在Android 1.6 (API Level 4)上,获取联系人的方法将fillMaps()实现为如下:

  1. private List<HashMap<String, String>> fillMaps() {
  2. List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
  3. Cursor cur = null;
  4. try {
  5. // Form an array specifying which columns to return.
  6. String[] projection = new String[] {
  7. People._ID,
  8. People.NAME,
  9. People.NUMBER
  10. };
  11. // query using ContentResolver.query or Activity.managedQuery
  12. cur = getContentResolver().query(
  13. People.CONTENT_URI,projection, null,null, null);
  14. if (cur.moveToFirst()) {
  15. String name;
  16. String phoneNumber;
  17. int nameColumn = cur.getColumnIndex(People.NAME);
  18. int phoneColumn = cur.getColumnIndex(People.NUMBER);
  19. do {
  20. // Get the field values
  21. name = cur.getString(nameColumn);
  22. phoneNumber = cur.getString(phoneColumn);
  23. // Do something with the values.
  24. HashMap<String, String> i = new HashMap<String, String>();
  25. i.put("name", name);
  26. i.put("key", phoneNumber);
  27. items.add(i);
  28. } while (cur.moveToNext());
  29. } else {
  30. HashMap<String, String> i = new HashMap<String, String>();
  31. i.put("name", "Your Phone");
  32. i.put("key", "Have No Contacts.");
  33. items.add(i);
  34. }
  35. } finally {
  36. if (cur != null)
  37. cur.close();
  38. }
  39. return items;
  40. }

复制代码

那么就能在1.6上运行了,效果截图如下:

<ignore_js_op>

联系人API,在Android2.0后产生变化,如果使用如上1.6版本的调用,你会发现在2.1下姓名有了,但电话号码不显示了。仔细观察可以发现,People.CONTENT_URI等调用在2.0以上的sdk中都标记了Deprecated。这一点,对于编写希望能够同时兼容1.6与2.x版本的应用造成了困难。那么,如果应用涉及到联系人的读取,非得要编写多个版本的apk了吗? 其实,我们可以使用判断当前系统API Level的方法编写两套代码备用,这个就留给大家去实践了。

获得系统API level方法:

  1. int version = android.provider.Settings.System.getInt(context
  2. .getContentResolver(),
  3. android.provider.Settings.System.SYS_PROP_SETTING_VERSION,
  4. 3);

复制代码

读取手机中的联系人信息(android.provider.ContactsContract)的更多相关文章

  1. 在手机中预置联系人/Service Number

    代码分为两部分: Part One 将预置的联系人插入到数据库中: Part Two 保证预置联系人仅仅读,无法被编辑删除(在三个地方屏蔽对预置联系人进行编辑处理:联系人详情界面.联系人多选界面.新建 ...

  2. Android API之android.provider.ContactsContract

    android.provider.ContactsContract ContactsContract是联系人provider和app的contract.定义了已支持的URL和column.取代了之前的 ...

  3. Android API之android.provider.ContactsContract.Contacts

    android.provider.ContactsContract.Contacts 对应contacts数据表.RawContacts的一个聚合(aggregate)代表同一个人.每个人在数据表co ...

  4. Android API之android.provider.ContactsContract.RawContacts

    android.provider.ContactsContract.RawContacts Constants for the raw contacts table, which contains o ...

  5. Android API之android.provider.ContactsContract.Data

    android.provider.ContactsContract.Data Constants for the data table, which contains data points tied ...

  6. Android 手机卫士--获取联系人信息并显示与回显

    前面的文章已经实现相关的布局,本文接着进行相关的功能实现 本文地址:http://www.cnblogs.com/wuyudong/p/5951794.html,转载请注明出处. 读取系统联系人 当点 ...

  7. 读取iPhone中的通讯录信息

    添加AddressBook这个包:然后#import <AddressBook/AddressBook.h> //取得本地通信录名柄 ABAddressBookRef addressBoo ...

  8. 【转】Revit二次开发——读取cad中的文字信息

    Revit读取cad的文字信息需要借助Teigha的开源dll,在程序中添加下图中红色框的dll文件的引用,其他的dll文件全部放在同一个文件夹中即可,运行的时候,会自动把这些dll文件全部复制到bi ...

  9. IOS 获取系统通讯录中的联系人信息

    - (IBAction)getAllContactFromSystem { ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NUL ...

随机推荐

  1. PHP symlink() 函数

    定义和用法 symlink() 函数创建一个从指定名称连接的现存目标文件开始的符号连接. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE. 语法 symlink(target,link) ...

  2. zabbix 邮件报警和微信报警

    # 邮件报警 一.定义邮件发件人 #密码来源 完成操作会看到 二.定义邮件收件人 三.启动动作 #先开启 2.触发操作 3.恢复操作 4.开启发送消息 1.2. 微信报警 一. 首先要注册一个企业微信 ...

  3. 使用ProxySQL实现MySQL Group Replication的故障转移、读写分离(二)

    在上一篇文章<使用ProxySQL实现MySQL Group Replication的故障转移.读写分离(一) > 中,已经完成了MGR+ProxySQL集群的搭建,也测试了ProxySQ ...

  4. 实验02——java两个数交换的三种解决方案

    package cn.tedu.demo;/** * @author 赵瑞鑫      E-mail:1922250303@qq.com * @version 1.0* @创建时间:2020年7月16 ...

  5. 经典的IPC问题

    Inter-Process Communication的缩写,含义是进程间通信,是指两个进程间交换数据的过程. 哲学家进餐问题 概述 哲学家进餐/思考 进餐需要两把叉子 每次拿一把叉子 如何预防死锁 ...

  6. 剑指Offer顺时针打印矩阵

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数 ...

  7. 防御sqlmap攻击之动态代码防御机制

    本文首发于“合天智汇”公众号 作者:SRainbow 关于动态代码防御机制,是自己瞎取的名字,目前我还没有看到过类似的文章.如果有前辈已经发表过,纯属巧合!!!我仅是突发奇想的一个想法,说不上高大上. ...

  8. 如何实现字符串转换成整数(实现atoi内置函数)?

    题目描述 输入一个由数字组成的字符串,把它转换成整数并输出.例如:输入字符串"123",输出整数123. 给定函数原型int StrToInt(const char *str) , ...

  9. 拼接html不显示layui进度条解决方法

    最新有个新需求,要异步拼接html并渲染数据,并且我这边是用layui的flow.load(流加载)渲染多个进度条.按官网给出的 element.progress('demo', n+'%'); 就是 ...

  10. OAuth2.0分布式系统环境搭建

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 介绍 OAuth(开放授权)是一 ...