Android 獲得聯絡人並排序
阿新 • • 發佈:2019-01-07
方式一:通過觀察匯出的聯絡人資料庫的相關欄位,根據欄位去查詢(在有些手機上會查詢不全,並且該類沒有實現排序)
import android.content.Context; import android.database.Cursor; import android.net.Uri; import com.wjustudio.phoneManager.Common.AppConstants; import com.wjustudio.phoneManager.javaBean.Contact; import java.util.ArrayList; /** * 作者:songwenju on 2016/4/2 22:00 * 郵箱:
[email protected] */ public class ContactUtil { /** * 獲得系統聯絡人 * * @param context * @return */ public static ArrayList<Contact> getContact(Context context) { ArrayList<Contact> contacts = new ArrayList<>(); Contact contact; String[] projection = {"data1", "mimetype"}; Cursor rawCursor = context.getContentResolver().query(Uri.parse(AppConstants.RAW_CONTACTS), new String[]{"contact_id"}, null, null, null); if (rawCursor != null && rawCursor.getCount() > 0) { LogUtil.i("ContactUtil", "the number of contact:" + rawCursor.getCount()); while (rawCursor.moveToNext()) { String contact_id = rawCursor.getString(rawCursor.getColumnIndex("contact_id")); LogUtil.i("contactUtil","contact_id:"+contact_id); if (contact_id != null) { Cursor dataCursor = context.getContentResolver().query( Uri.parse(AppConstants.DATA_CONTACTS), projection, "raw_contact_id = ?", new String[]{contact_id}, null); if (dataCursor != null && dataCursor.getCount() > 0) { contact = new Contact(); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1")); data1 = data1.trim().replaceAll("-","").replaceAll(" ",""); String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype")); switch (mimetype) { case "vnd.android.cursor.item/name": contact.contact_name = data1; if (ChineseUtil.isChinese(data1.charAt(0))){ contact.pinYin = PinYinUtil.toPinyin(data1); }else { contact.pinYin = data1; } break; case "vnd.android.cursor.item/email_v2": contact.email = data1; break; case "vnd.android.cursor.item/phone_v2": contact.contact_phoneNum = data1; break; } } contacts.add(contact); } } } } LogUtil.e("contactUtil","contactNum:"+contacts.size()); return contacts; } }
對應的bean
package com.wjustudio.contactdemo; import java.util.ArrayList; import java.util.List; /** * 作者:songwenju on 2016/4/2 22:01 * 郵箱:[email protected] */ public class Contact { public String name; public String pinYin; public List<String> telephoneNumber = new ArrayList<>(); public String email; @Override public String toString() { return "Contact{" + "name='" + name + '\'' + ", pinYin='" + pinYin + '\'' + ", telephoneNumber=" + telephoneNumber + ", email='" + email + '\'' + '}'; } }
對應的AppConstants中兩個欄位
/**
* App所用的常量
* 作者:songwenju on 2016/1/31 11:39
* 郵箱:[email protected]
*/
public class AppConstants {
public static final String RAW_CONTACTS = "content://com.android.contacts/raw_contacts";
public static final String DATA_CONTACTS = "content://com.android.contacts/data";
}
方式二:通過系統提供的欄位來獲取資料,並實現了按拼音排序
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import com.wjustudio.phoneManager.javaBean.Contact;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import java.util.ArrayList;
public class ContactUtils {
public static ArrayList<Contact> getContact(Context context) {
ArrayList<Contact> listMembers = new ArrayList<>();
Cursor cursor = null;
try {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
// 這裡是獲取聯絡人表的電話裡的資訊 包括:名字,聯絡人id,電話號碼;
// 然後在根據"sort-key"排序
cursor = context.getContentResolver().query(
uri,
new String[] { "display_name", "contact_id", "data1" },
null, null, "sort_key");
if (cursor != null && cursor.moveToFirst()) {
LogUtil.e("contactUtil", "ContactCount:" + cursor.getCount());
while (cursor.moveToNext()){
Contact contact = new Contact();
String contact_phoneNum = cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
int contact_id = cursor.getInt(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
contact.contact_name = name;
contact.contact_phoneNum = contact_phoneNum.replaceAll("[^0-9]", "");
contact.contact_id = contact_id;
contact.pinYin = getPingYin(name);
LogUtil.e("contactUtils", "contact:" + contact.toString());
if (name != null)
listMembers.add(contact);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null){
cursor.close();
}
}
return listMembers;
}
/**
* 將字串中的中文轉化為拼音,其他字元不變
*
* @param inputString 輸入的中文漢字
* @return 獲得的拼音
*/
public static String getPingYin(String inputString) {
if (TextUtils.isEmpty(inputString)) {
return "";
}
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (char anInput : input) {
if (Character.toString(anInput).matches(
"[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(
anInput, format);
if (temp == null || TextUtils.isEmpty(temp[0])) {
continue;
}
output += temp[0].replaceFirst(temp[0].substring(0, 1),
temp[0].substring(0, 1).toUpperCase());
} else
output += Character.toString(anInput);
}
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
}
對應的bean
/**
* author:songwenju on 16-4-7 11 : 07
* Email: [email protected]
*/
public class Contact {
public String contact_name;
public String contact_phoneNum;
public int contact_id;
public String email;
//public List<String> telephoneNumber = new ArrayList<>();
public String pinYin;
@Override
public String toString() {
return "Contact{" +
"contact_name='" + contact_name + '\'' +
", contact_phoneNum='" + contact_phoneNum + '\'' +
", contact_id=" + contact_id +
", email='" + email + '\'' +
", pinYin='" + pinYin + '\'' +
'}';
}
}
聯絡人資料庫裡面聯絡人和電話號碼是分別存在兩個表裡面的,因為存在一個聯絡人擁有幾個號碼的情況,所以android為聯絡人和手機號碼分別單獨建立了相應的檢視。
聯絡人資訊的視圖裡面只儲存與聯絡人相關的資料,例如姓名,是否有手機號碼等。
而手機號碼資料則是每一個電話號碼為一條記錄,如果有一個聯絡人有3個號碼,則裡面會出現3個該聯絡人的記錄,號碼分別為他的三個號碼。
如果是需要讀取聯絡人資訊,傳入的URI為:ContactsContract.Contacts.CONTENT_URI
如果是需要讀取手機號碼資訊傳入的URI為:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
下篇文章將寫一個完整的小專案去實現讀聯絡人,並且以自定義View的形式展示出來。