android获取手机录

在Android开发中,读取手机通讯录中的号码是一种基本操作,但是由于Android的版本众多,所以手机通讯录操作的代码比较纷杂,在本文中进行一下总结。

Android1.5是现在的Android系统中最低的版本,首先来说一下适用于Android1.5及以上版本(含2.X,3.X)的代码实现:

//获得所有的联系人

Cursor cur = context.getContentResolver().query(

Contacts.People.CONTENT_URI,

null,

null,

null,

Contacts.People.DISPLAY_NAME +" COLLATE LOCALIZED ASC");

// 循环遍历

if (cur.moveToFirst()) {

int idColumn = cur.getColumnIndex(Contacts.People._ID);

int displayNameColumn = cur.getColumnIndex(Contacts.People.DISPLAY_NAME);

do {

// 获得联系人的ID号

String contactId =cur.getString(idColumn);

 

// 获得联系人姓名

String disPlayName =cur.getString(displayNameColumn);

 

//获取联系人的电话号码

CursorphonesCur = context.getContentResolver().query(

Contacts.Phones.CONTENT_URI,null,

Contacts.Phones.PERSON_ID+ "=" + contactId, null, null);

if (phonesCur.moveToFirst()) {

do {

// 遍历所有的电话号码

StringphoneType = phonesCur.getString(phonesCur

.getColumnIndex(Contacts.PhonesColumns.TYPE));

String phoneNumber =phonesCur.getString(phonesCur

.getColumnIndex(Contacts.PhonesColumns.NUMBER));

//自己的逻辑处理代码

}while(phonesCur.moveToNext());

}

}while (cur.moveToNext());

}

cur.close();

使用这段代码可以在各种版本的Android手机中读取手机通讯录中的电话号码,而且可以读取一个姓名下的多个号码,但是由于使用该代码在2.x版本中的效率不高,读取的时间会稍长一些,而且2.x现在是Android系统的主流,至少占有80%以上的Android手机份额,所以可以使用高版本的API进行高效的读取。

适用于Android2.0及以上版本的读取通讯录的代码如下:

//读取手机本地的电话

ContentResolver cr =context.getContentResolver();

//取得电话本中开始一项的光标,必须先moveToNext()

Cursor cursor =cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

while(cursor.moveToNext()){

//取得联系人的名字索引

int nameIndex =cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);

String name = cursor.getString(nameIndex);

//取得联系人的ID索引值

String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

//查询该位联系人的电话号码,类似的可以查询email,photo

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));

listName.add(name);

listPhone.add(phoneNumber);

}

phone.close();

}

cursor.close();

如果需要读取SIM卡里面的通讯录内容,则可以使用:”content://icc/adn”进行读取,代码如下:

try{

Intent intent = new Intent();

intent.setData(Uri.parse(“content://icc/adn”));

Uri uri = intent.getData();

ContentResolvercr = context.getContentResolver();

Cursor cursor =context.getContentResolver().query(uri, null, null, null, null);

if (cursor != null) {

while(cursor.moveToNext()){

//取得联系人的名字索引

int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);

String name = cursor.getString(nameIndex);

//取得联系人的ID索引值

String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

//查询该位联系人的电话号码,类似的可以查询email,photo

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));

//自己的逻辑代码

}

phone.close();

}

cursor.close();

}

}catch(Exception e){}


通讯录上的信息,存储在两个地方,一个是SIM卡,一个是手机本地,

首先是手机本地:
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里面,然后砸显示的是采用适配器,这样就完成了

在写的时候,一定要注意获取的方式。

 

转载于:https://www.cnblogs.com/qiaoxu/p/3990094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值