现在的位置: 首页 > 综合 > 正文

android 根据号码判断联系人是否存在

2012年12月14日 ⁄ 综合 ⁄ 共 2904字 ⁄ 字号 评论关闭

联系人存储包括两个位置: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.

Android rawContact数据查询

2 判断是否存在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();
}
}

3 打开联系人详情页面(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));
}

抱歉!评论已关闭.