您的位置:首页 > 移动开发 > Android开发

[转载] Android 2.3.3 API 读取通讯录中电话号码的实例

2012-06-14 10:41 477 查看
最近开始学习Android,主要看的是《Android应用开发揭秘》,在第3章的Example_03_02是一个读取通讯录联系人姓名和电话的实例,但由于API 2.0中,每个联系人可以有多个电话(例如手机、住宅、公司、传真等),书中原有的实例在API 2.0的环境中会报错。

书中的Example_03_02代码:

View Code

package com.yarin.android.Examples_03_02;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.widget.TextView;

import org.apache.commons.lang.StringUtils;

public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

Log.i("tag",Integer.toString(cursor.getColumnCount())) ;
Log.i("tag",StringUtils.join(cursor.getColumnNames(), ",") ) ;
//向下移动一下光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码 (此方法会报错,因为一个联系人可能有多个联系电话)
//int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup._ID);
//String number = cursor.getString(numberFieldColumnIndex);
//string += (contact+":"+number+"\n");

//新方法获取电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"="+ ContactId, null, null);
while(phone.moveToNext())
{
String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (contact +":"+ PhoneNumber +"\n");
}

}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}


其中的原代码已被我注解,并改了正确的代码

如果我们只想获取手机号如何操作呢?

Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);


把以上语句改为:

Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId + " AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + "=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, null, null);


ContactsContract.CommonDataKinds.Phone.TYPE 表示联系人电话的类型,主要对应如下:
TYPE_MOBILE : 手机号码
TYPE_HOME : 住宅电话
TYPE_WORK : 公司电话

最后附加main.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐