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));
}