一 对SIM卡联系人插入过程,其实分为两部分来完成,第一部分是插入SIM卡里存储的联系人,第二部分是插入到本地数据库里。只有当这两个步骤都完成时,才能完成SIM卡联系人的插入。
1 我们就从Android4.x的源码来分析下,源码一定能淋漓尽致的来说明此问题。
插入过程从下列开始ContactEditorFragment来展开说明:
当用户来插入联系人时,点击完成时会调用ContactEditorFragment类里面的
public boolean save(int saveMode) {
if (!hasValidState() || mStatus != Status.EDITING) {
return false;
}
// If we are about to close the editor - there is no need to refresh the data
if (saveMode == SaveMode.CLOSE || saveMode == SaveMode.SPLIT) {
getLoaderManager().destroyLoader(LOADER_DATA);
}
mStatus = Status.SAVING;
if (!hasPendingChanges()) {
if (mLookupUri == null && saveMode == SaveMode.RELOAD) {
// We don't have anything to save and there isn't even an existing contact yet.
// Nothing to do, simply go back to editing mode
mStatus = Status.EDITING;
return true;
}
onSaveCompleted(false, saveMode, mLookupUri != null, mLookupUri,
getActivity().getIntent().getIntExtra(ContactSaveService.SAVE_CONTACT_RESULT, 0));
return true;
}
setEnabled(false);
// Store account as default account, only if this is a new contact
saveDefaultAccountIfNecessary();
// Save contact
Intent intent = ContactSaveService.createSaveContactIntent(mContext, mState,
SAVE_MODE_EXTRA_KEY, saveMode, isEditingUserProfile(),
((Activity)mContext).getClass(), ContactEditorActivity.ACTION_SAVE_COMPLETED,
mUpdatedPhotos);
mContext.startService(intent);
// Don't try to save the same photos twice.
mUpdatedPhotos = new Bundle();
return true;
}
点击确定后就执行了该方法,该方法里真正执行插入联系人的方法是
Intent intent = ContactSaveService.createSaveContactIntent(mContext, mState,
SAVE_MODE_EXTRA_KEY, saveMode, isEditingUserProfile(),
((Activity)mContext).getClass(), ContactEditorActivity.ACTION_SAVE_COMPLETED,
mUpdatedPhotos);
mContext.startService(intent);
创建了一个Intent来启动一个服务来完成联系人的插入全部过程。
接下来我们来研究下该服务是如何来保存联系人的,首先该服务是继承IntentService ,具体IntentService 个service有什么不同自己百度可以了解,在此就不多说了
直接上代码了
protected void onHandleIntent(Intent intent) {
// Call an appropriate method. If we're sure it affects how incoming
// phone calls are
// handled, then notify the fact to in-call screen.
String action = intent.getAction();
if (ACTION_NEW_RAW_CONTACT.equals(action)) {
createRawContact(intent);
CallerInfoCacheUtils.sendUpdateCallerInfoCacheIntent(this);
} else if (ACTION_SAVE_CONTACT.equals(action)) {
saveContact(intent);
CallerInfoCacheUtils.sendUpdateCallerInfoCacheIntent(this);
} else if (ACTION_CREATE_GROUP.equals(action)) {
当接收到ACTION_SAVE_CONTACT这个action后开始保存联系人
private void saveContact(Intent intent) {
RawContactDeltaList state = intent
.getParcelableExtra(EXTRA_CONTACT_STATE);
boolean isProfile = intent
.getBooleanExtra(EXTRA_SAVE_IS_PROFILE, false);
Bundle updatedPhotos = intent.getParcelableExtra(EXTRA_UPDATED_PHOTOS);
// Trim any empty fields, and RawContacts, before persisting
final AccountTypeManager accountTypes = AccountTypeManager
.getInstance(this);
RawContactModifier.trimEmpty(state, accountTypes);
Uri lookupUri = null;
final ContentResolver resolver = getContentResolver();
boolean succeeded = false;
// Keep track of the id of a newly raw-contact (if any... there can be
// at most one).
long insertedRawContactId = -1;
// Attempt to persist changes
Integer result = RESULT_FAILURE;
boolean isCardOperation = false;
for (int i = 0; i < state.size(); i++) {
final RawContactDelta entity = state.get(i);
final String accountType = entity.getValues().getAsString(
RawContacts.ACCOUNT_TYPE);
final String accountName = entity.getValues().getAsString(