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

本文介绍如何在Android 2.0及更高版本中访问和读取设备上的联系人信息。通过示例代码展示了如何使用SQLite和Cursor从Android Contacts数据库中获取联系人姓名和电话号码,并将这些信息展示在ListView中。

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 {
	  
	@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的使用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值