在android中读取所有联系人的电话号码 [英] Read all contacts' phone numbers in android

查看:114
本文介绍了在android中读取所有联系人的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来检索所有联系人姓名和电话号码:

I'm using this code to retrieve all contact names and phone numbers:

String[] projection = new String[]
{
    People.NAME,
    People.NUMBER
};

Cursor c = ctx.getContentResolver().query(People.CONTENT_URI, projection, null, null, People.NAME + " ASC");
c.moveToFirst();

int nameCol = c.getColumnIndex(People.NAME);
int numCol = c.getColumnIndex(People.NUMBER);

int nContacts = c.getCount();

do
{
  // Do something
} while(c.moveToNext());

但是,这只会返回每个联系人的主要电话号码,但是我也想获取次要电话号码.我该怎么办?

However, this will only return the primary number for each contact, but I want to get the secondary numbers as well. How can i do this?

推荐答案

您可以通过以下方式读取与联系人关联的所有电话号码:

You can read all of the telephone numbers associated with a contact in the following manner:

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

请注意,此示例(与您的示例一样)使用了不推荐使用的联系人API.从eclair开始,它已被 ContactsContract API取代

Please note that this example (like yours) uses the deprecated contacts API. From eclair onwards this has been replaced with the ContactsContract API.

这篇关于在android中读取所有联系人的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆