android 根據號碼判斷連絡人是否存在

來源:互聯網
上載者:User

連絡人儲存包括兩個位置: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));
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.