Android 4.x SIM卡联系人插入过程

一 对SIM卡联系人删除过程,其实分为两部分来完成,
摘要由CSDN通过智能技术生成

一 对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(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值