Android determines whether a contact exists based on the number.

Source: Internet
Author: User

The contact storage includes two locations: SIM card and mobile phone. You must determine the contact storage space during the search process.

The mobile phone is stored in/data/COM/Android. providers. Contacts/databases.

1. Determine whether the data is stored on the mobile phone (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();
}

You can also search in the following ways:

Cursor cursor = This. getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri,
Projection, // return Field
Contactscontract. commondatakinds. Phone. Number + "= '" + mnumber + "'",//
Null, // where clause value substitution
Null); // sort order.

Android rawcontact Data Query

2. Determine whether the SIM card exists (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. Open the contact details page (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));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.