Android Contacts(一)—— 读取联系人
来源:互联网 发布:类似美工云的网站 编辑:程序博客网 时间:2024/06/01 10:03
IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!
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 读取代码:
- 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<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 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" />
运行结果:
代码示例
参考推荐:
Working With Android Contacts
Android Contacts的使用
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- (转)Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android源码——通讯录联系人(一):Contacts数据库初识
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- (转)Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android 学习笔记 Contacts (二)Contacts 联系人详解
- Android 学习笔记 Contacts (二)Contacts 联系人详解
- Android Service 服务(二)—— BroadcastReceiver
- bash重拾
- Android Service 服务(一)—— Service
- 如何实现P2P穿透
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(一)—— 读取联系人
- 黑马程序员-java基础之排序、查找、内存分配、函数重载
- Android SMS(二)—— 读取短信保存到 SQLite
- H.264 无参考视频质量评价方法 (使用了基于遗传编程方法的符号回归)
- tomcat服务的配置和开启
- 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数 要求下排每个数都是先前上排那十个数在下排出现的次数
- Android SMS(一) —— 读取短信
- 银行业务调度系统
- Android 国内应用市场的汇总