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 theAndroid SQLite and Cursor Articlefor 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 读取代码:

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 {  @Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);showListView();}private void showListView(){ListView listView = new ListView(this);ArrayList<HashMap<String, String>> 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<HashMap<String, String>> getPeopleInPhone2(){ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);// 获取手机联系人while (cursor.moveToNext()) {HashMap<String, String> map = new HashMap<String, String>();int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); // people nameint indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER); // phone numberString 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" />


运行结果:



代码示例


参考推荐:

Working With Android Contacts

Android Contacts的使用


更多相关文章

  1. Android(安卓)Contacts(一)—— 读取联系人
  2. Android学习感想一
  3. [原]Android有用代码片断(六)
  4. android shape 代码实现按钮背景
  5. android shape 代码实现按钮背景
  6. [笔记分享] [Android] Android系统概述
  7. android与javascript交互调用
  8. Android(安卓)使用Vitamio打造自己的万能播放器(4)――本地播放(快
  9. Android(安卓)文件的保存与读取之SDCard(SD卡)存储

随机推荐

  1. android button多状态, selector
  2. 修改ScrollView滚动条样式
  3. Android Studio Toast/Notification中文
  4. Android Overview
  5. Android的Adapter与BaseAdapter的介绍
  6. Android 控件属性
  7. Android练习——Spinner二级联动_城市选
  8. Android屏幕尺寸、标题栏高度、状态栏高
  9. Android解析json数组对象
  10. android SAX 解析 xml文档