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 ?
  1. package com.homer.phone;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.database.Cursor;  
  8. import android.os.Bundle;  
  9. import android.provider.ContactsContract;  
  10. import android.provider.ContactsContract.CommonDataKinds.Phone;  
  11. import android.widget.ListView;  
  12. import android.widget.SimpleAdapter;  
  13.   
  14. public class phoneRead extends Activity {  
  15.         
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState){  
  18.         super.onCreate(savedInstanceState);  
  19.           
  20.         showListView();  
  21.     }  
  22.       
  23.     private void showListView(){  
  24.         ListView listView = new ListView(this);  
  25.           
  26.         ArrayList> list = getPeopleInPhone2();  
  27.         SimpleAdapter adapter = new SimpleAdapter(  
  28.                                     this,   
  29.                                     list,   
  30.                                     android.R.layout.simple_list_item_2,   
  31.                                     new String[] {"peopleName""phoneNum"},   
  32.                                     new int[]{android.R.id.text1, android.R.id.text2}  
  33.                                 );  
  34.         listView.setAdapter(adapter);  
  35.           
  36.         setContentView(listView);  
  37.     }  
  38.       
  39.     private ArrayList> getPeopleInPhone2(){  
  40.         ArrayList> list = new ArrayList>();  
  41.           
  42.         Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnullnullnull);     // 获取手机联系人  
  43.         while (cursor.moveToNext()) {  
  44.             HashMap map = new HashMap();  
  45.               
  46.             int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);    // people name  
  47.             int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);            // phone number  
  48.   
  49.             String strPeopleName = cursor.getString(indexPeopleName);  
  50.             String strPhoneNum = cursor.getString(indexPhoneNum);  
  51.   
  52.             map.put("peopleName", strPeopleName);  
  53.             map.put("phoneNum", strPhoneNum);  
  54.             list.add(map);  
  55.         }  
  56.         if(!cursor.isClosed()){  
  57.             cursor.close();  
  58.             cursor = null;  
  59.         }  
  60.           
  61.         return list;  
  62.     }  
  63. }  

AndroidManifest.xml 权限

 记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission


运行结果:




转自:http://blog.csdn.net/ithomer/article/details/7328590


更多相关文章

  1. android操作通讯录的联系人
  2. 详细介绍一个.net开源权限管理系统
  3. 用户管理和权限和设置——mysql
  4. 基于Spring Security Oauth2的SSO单点登录+JWT权限控制实践
  5. 基于Spring Security和 JWT的权限系统设计
  6. Centos给文件设置了777权限仍不能访问解决方案
  7. 一言不合就改成 777 权限?会出人命的!
  8. centos 普通用户使用root的权限
  9. centos 用户权限管理与文件权限设定 详解

随机推荐

  1. Android(安卓)FileUtil
  2. AudioFormat音频格式
  3. Android(安卓)View深入学习(一),View的测量(M
  4. AndroidStudio2.2-2.3安装不了anko-plugi
  5. 如何把应用跑在android上
  6. Android(安卓)getSystemService用法实例
  7. Android开发环境安装
  8. Android(安卓)View事件传递详解
  9. reactnative 在Android上添加阴影效果
  10. android 广播BroadcastReveicer详解