Android Contacts(一)—— 读取联系人
2014-10-03 15:45
309 查看
Introduction To Android Contacts
Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the AndroidSQLite and Cursor Article for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with
accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.
Android联系人的介绍
学习使用Android联系人的数据库。假设你已经了解在Android中通过Cursor使用SQLite的基本知识,更多信息请查阅 “Android SQLite and Cursor Article” 。Google在Android升级到2.0时对联系人数据库进行了修改,该教程将分为3个章节:1.在Android2.0中获取联系人数据;2.在Android 1.6及之前版本中获取联系人的方法;3.通过一个抽象类对各版本获取联系人数据的操作进行统一,总结一系列用来管理从通讯录中获取到的数据的类。
(翻译部分为yxyx1024原创,如有不妥之处还望包涵并指正,感谢。)
Contacts 读取代码:
package com.homer.phone; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.widget.ListView; import android.widget.SimpleAdapter; public class phoneRead extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); showListView(); } private void showListView(){ ListView listView = new ListView(this); ArrayList> list = getPeopleInPhone2(); SimpleAdapter adapter = new SimpleAdapter( this, list, android.R.layout.simple_list_item_2, new String[] {"peopleName", "phoneNum"}, new int[]{android.R.id.text1, android.R.id.text2} ); listView.setAdapter(adapter); setContentView(listView); } private ArrayList> getPeopleInPhone2(){ ArrayList> list = new ArrayList>(); Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); // 获取手机联系人 while (cursor.moveToNext()) { HashMap map = new HashMap(); int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); // people name int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER); // phone number String strPeopleName = cursor.getString(indexPeopleName); String strPhoneNum = cursor.getString(indexPhoneNum); map.put("peopleName", strPeopleName); map.put("phoneNum", strPhoneNum); list.add(map); } if(!cursor.isClosed()){ cursor.close(); cursor = null; } return list; } }
AndroidManifest.xml 权限
记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission
<uses-permission android:name="android.permission.READ_CONTACTS" />
运行结果:
![](http://hi.csdn.net/attachment/201203/20/0_1332203900Qti5.gif)
代码示例
参考推荐:
Working With Android Contacts
Android Contacts的使用
(本文转自http://blog.csdn.net/ithomer/article/details/7328590)
相关文章推荐
- Android Contacts(一)—— 读取联系人
- Android使用READ_CONTACTS读取手机联系人
- Android中读取电话本Contacts联系人的所有电话号信息
- 读取手机中的联系人信息(android.provider.ContactsContract)
- [每天学点Android开发]使用新的Contacts API (ContactsContract)读取联系人信息
- Android中读取电话本Contacts联系人的所有电话号信息
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android使用READ_CONTACTS读取手机联系人
- Android2.0之后读取联系人——ContactsContract
- Android中读取电话本Contacts联系人的所有电话号信息
- (转)Android Contacts(一)—— 读取联系人
- Android中读取电话本Contacts联系人的所有电话号信息
- Android2.0 中读取联系人——ContactsContract
- Android读取联系人的姓名及号码
- 在android中读取联系人信息的程序,包括读取联系人姓名、联系方式和邮箱等
- android-AndroidContacts操作系列之 查看底层联系人表结构
- Android将联系人读取到LISTVIEW中遇到的问题!
- Android ContentResolver 使用例子,读取联系人
- Android读取联系人的信息