android擷取手機錄

來源:互聯網
上載者:User

標籤:android   io   os   使用   ar   sp   cti   on   c   

在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裡面,然後砸顯示的是採用適配器,這樣就完成了

在寫的時候,一定要注意擷取的方式。

 

android擷取手機錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.