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


运行结果:



代码示例


参考推荐:

Working With Android Contacts

Android Contacts的使用 



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

更多相关文章

  1. 第一行代码Android第三课
  2. Android 开源源代码收集(不断更新中...)
  3. Android顶部工具栏和底部工具栏的简单实现代码
  4. Android Activity的4种启动模式详解(示例)
  5. Android的多媒体框架Opencore代码阅读
  6. Android应用程序启动过程源代码分析
  7. Android中创建对话框(确定取消对话框、单选对话框、多选对话框)

随机推荐

  1. android-scripting - Scripting Layer fo
  2. 生成android的bks证书
  3. android中关于Sqlite的问题
  4. Android(安卓)(shape,gradient)使用总结
  5. Android(安卓)异步获取网络图片并处理图
  6. Android通过shape.xml制作渐变背景
  7. Android高德地图获取当前地理位置(不显示
  8. Unity3D在android下调试
  9. 常用的android开发网站
  10. android触控,先了解MotionEvent