|
Android中读取电话本Contacts联系人的所有电话号信息
通讯录上的信息,存储在两个地方,一个是SIM卡,一个是手机本地, 这个大家首先要明确。我们读取联系人就要读取两个地方,因为我一直都在用2.2的SDK开发,所以这个关于1.5的是在网上找的,还不错,就拿过来给大家看看。因为现在好像是在公司开发还是以尽量兼容1.6的SDK为主,并没有达到兼容2.2SDK这么高呢。可能有的朋友会用到。
ContactsContract.Contacts.CONTENT_URI是2.1下的Contacts表查询Uri;1.5的应该是Contacts.People.CONTENT_URI
首先是手机本地:
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
ContactInfo cci = new ContactInfo();
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(People.NAME);
cci.contactName = cursor.getString(nameFieldColumnIndex);
//取得电话号码
int numberFieldColumnIndex = cursor.getColumnIndex(People.NUMBER);
cci.userNumber = cursor.getString(numberFieldColumnIndex);
cci.userNumber = GetNumber(cci.userNumber);
cci.isChecked = false;
if (IsUserNumber(cci.userNumber)) {
if (!IsContain(contactList, cci.userNumber)) {
if(IsAlreadyCheck(wNumStr, cci.userNumber)){
cci.isChecked = true;
numberStr += "," + cci.userNumber;
}
contactList.add(cci);
//Log.i("eoe", "*********"+cci.userNumber);
}
}
}
cursor.close();
}
下面是获取SIM卡:
//从SIM卡中取号
private void GetSimContact(String add){
//读取SIM卡手机号,有两种可能:content://icc/adn与content://sim/adn
try {
Intent intent = new Intent();
intent.setData(Uri.parse(add));
Uri uri = intent.getData();
mCursor = getContentResolver().query(uri, null, null, null, null);
if (mCursor != null) {
while (mCursor.moveToNext()) {
ContactInfo sci = new ContactInfo();
// 取得联系人名字
int nameFieldColumnIndex = mCursor.getColumnIndex("name");
sci.contactName = mCursor.getString(nameFieldColumnIndex);
// 取得电话号码
int numberFieldColumnIndex = mCursor
.getColumnIndex("number");
sci.userNumber = mCursor.getString(numberFieldColumnIndex);
sci.userNumber = GetNumber(sci.userNumber);
sci.isChecked = false;
if (IsUserNumber(sci.userNumber)) {
if (!IsContain(contactList, sci.userNumber)) {
if(IsAlreadyCheck(wNumStr, sci.userNumber)){
sci.isChecked = true;
numberStr += "," + sci.userNumber;
}
contactList.add(sci);
//Log.i("eoe", "*********"+sci.userNumber);
}
}
}
mCursor.close();
}
} catch (Exception e) {
Log.i("eoe", e.toString());
}
}
以上是将获取到的信息对象方法ArrayList<ContactInfo> contactList里面,然后砸显示的是采用适配器,这样就完成了
在写的时候,一定要注意获取的方式。 |
|