(转)Android Contacts(一)—— 读取联系人
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 Android SQLite 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.
Contacts 读取代码:
[java] view plain copy print ?
- 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
运行结果:
代码示例
参考推荐:
Working With Android Contacts
Android Contacts的使用
转自:http://blog.csdn.net/sunboy_2050/article/details/7328590
更多相关文章
- 第一行代码Android第三课
- Android 开源源代码收集(不断更新中...)
- Android顶部工具栏和底部工具栏的简单实现代码
- Android Activity的4种启动模式详解(示例)
- Android的多媒体框架Opencore代码阅读
- Android应用程序启动过程源代码分析
- Android中创建对话框(确定取消对话框、单选对话框、多选对话框)