联系人存储包括两个位置:SIM卡和手机上,在查找过程中要分别判断。
手机上存储位置在/data/data/com/android.providers.contacts/databases。
1 判断是否存储在手机上(CallDetailActivity)
Uri personUri = null;
Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(mNumber));
Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
try {
if (phonesCursor != null && phonesCursor.moveToFirst()) {
long personId = phonesCursor.getLong(COLUMN_INDEX_ID);
personUri = ContentUris.withAppendedId(
Contacts.CONTENT_URI, personId);
callText = getString(R.string.recentCalls_callNumber,
phonesCursor.getString(COLUMN_INDEX_NAME));
mNumber = PhoneNumberUtils.formatNumber(
phonesCursor.getString(COLUMN_INDEX_NUMBER));
callLabel = Phone.getDisplayLabel(this,
phonesCursor.getInt(COLUMN_INDEX_TYPE),
phonesCursor.getString(COLUMN_INDEX_LABEL)).toString();
} else {
mNumber = PhoneNumberUtils.formatNumber(mNumber);
}
} finally {
if (phonesCursor != null) phonesCursor.close();
}
还可以用以下几种方式搜索:
Cursor cursor = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, //返回字段
ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + mNumber + "'", //
null, // WHERE clause value substitution
null); // Sort order.
判断是否存在SIM卡上(CallDetailActivity)
Cursor simCursor=null;
String[] SIM_CONTENT_PROJECTION = new String[] {
"name", "number", };
boolean hasIccCard1 = ((TelephonyManager) this .getSystemService(
PhoneFactory.getServiceName(Context.TELEPHONY_SERVICE, 0))).hasIccCard();
if(hasIccCard1){
simCursor = resolver.query(EditSimCardActivity.SIM1_URI,SIM_CONTENT_PROJECTION, null, null, null);
try {
if (simCursor != null) {
if (simCursor.moveToFirst()) {
do {
String tempName = simCursor.getString(0);
String tempNumber = simCursor.getString(1);
if (mNumber.equals(tempNumber)) {
hasFoundContact=true;
simContactIntent=new Intent();
simContactIntent.setClass(this, ViewSimCardContactActivity.class);
simContactIntent.putExtra(EditSimCardActivity.SIM_CONTACT_NAME, tempName);
simContactIntent.putExtra(EditSimCardActivity.SIM_CONTACT_NUMBER, tempNumber);
simContactIntent.putExtra(EditSimCardActivity.SIM_ADDRESS, EditSimCardActivity.SIM1_ADDRESS);
callText = getString(R.string.recentCalls_callNumber,tempName);
mNumber = PhoneNumberUtils.formatNumber(tempNumber);
break;
}
} while (simCursor.moveToNext());
}
simCursor.close();
}
} finally {
if (simCursor != null) simCursor.close();
}
}
打开联系人详情页面(CallDetailActivity)
if (personUri != null) { //phone
Intent viewIntent = new Intent(Intent.ACTION_VIEW, personUri);
actions.add(new ViewEntry(R.drawable.sym_action_view_contact,
getString(R.string.menu_viewContact), viewIntent));
} else if (simContactIntent != null) { //sim
actions.add(new ViewEntry(R.drawable.sym_action_view_contact,
getString(R.string.menu_viewContact), simContactIntent));
} else { // none
Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
createIntent.setType(Contacts.CONTENT_ITEM_TYPE);
createIntent.putExtra(Insert.PHONE, mNumber);
actions.add(new ViewEntry(R.drawable.sym_action_add,
getString(R.string.recentCalls_addToContact), createIntent));
}
拉起联系人修改或添加界面
//android.intent.action.INSERT_OR_EDIT
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
//vnd.android.cursor.item/contact
intent.setType(Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(Insert.PHONE, number);
直接拉起添加页面
Intent intent = new Intent(Intent.ACTION_INSERT,
Uri.withAppendedPath(
Uri.parse("content://com.android.contacts"),
"contacts"));
intent.putExtra(Intents.Insert.PHONE, number);
查询联系人并区分存储位置
private void initDate() {
// 查找所有联系人
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int contactIdIndex = 0;
int nameIndex = 0;
if (cursor.getCount() > 0) {
DatabaseUtils.dumpCursor(cursor);
contactIdIndex = cursor
.getColumnIndex(ContactsContract.Contacts._ID);
nameIndex = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
}
while (cursor.moveToNext()) {
String contactId = cursor.getString(contactIdIndex);
String name = cursor.getString(nameIndex);
/*
* 查找该联系人的phone信息
*/
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contactId, null, null);
int phoneIndex = 0;
int accouttypeIndex = 0;
if (phones.getCount() > 0) {
DatabaseUtils.dumpCursor(phones);
phoneIndex = phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
accouttypeIndex = phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
}
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phoneIndex);
int AccountType = phones.getInt(accouttypeIndex);
// 1 存在手机 ;2 存在sim卡上
if(1 == AccountType){
ImportContactBean bean = new ImportContactBean(contactId, name, phoneNumber);
mContactlist.add(bean);
}
}
if (phones != null) {
phones.close();
}
}
if (cursor != null) {
cursor.close();
}
}