前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Contacts(一)—— 读取联系人

Android Contacts(一)—— 读取联系人

作者头像
阳光岛主
发布2019-02-19 17:14:30
9550
发布2019-02-19 17:14:30
举报
文章被收录于专栏:米扑专栏米扑专栏

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 读取代码:

代码语言:javascript
复制
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的使用

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012年03月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Introduction To Android Contacts
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档