• Android 编辑联系人


        /**
         * @brief 显示联系人编辑界面
         * */
        public void editContact(String contactId,Context context) {
    
            if (context == null || contactId == null) {
                return ;
            }
    
            Uri uri = getContactUri(contactId,context);
            if (uri != null) {
    
                Intent editIntent = new Intent(Intent.ACTION_EDIT);
                editIntent.setDataAndType(uri,ContactsContract.Contacts.CONTENT_ITEM_TYPE);
    //            editIntent.putExtra("finishActivityOnSaveCompleted", true);
                context.startActivity(editIntent);
    
            } else {
    
            }
        }
    
        /**
         * 修改联系人信息
         * */
        public void updateContact(Contact contactOld, Contact contactNew) {
            Log.w(TAG, "**update start**");
            String id = getContactID(contactOld.getName());
            if(id.equals("0")) {
                Log.d(TAG, contactOld.getName()+" not exist.");
            }/* else if(contactNew.getName().trim().equals("")){
                Log.d(TAG, "contact name is empty. exit.");
            } else if(!getContactID(contactNew.getName()).equals("0")){
                Log.d(TAG, "new contact name already exist. exit.");
            }*/ else {
    
                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    
                //update name
    //            ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
    //                    .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?",
    //                            new String[]{id, MIMETYPE_STRING_NAME})
    //                    .withValue(COLUMN_NAME, contactNew.getName())
    //                    .build());
                Log.d(TAG, "update name: " + contactNew.getName());
    
                // photo
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?",
                                new String[]{id, MIMETYPE_STRING_PHOTO})
                        .withValue(COLUMN_PHOTO, contactNew.getPhotoData())
                        .build());
    
                // web
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?",
                                new String[]{id, MIMETYPE_STRING_WEB})
                        .withValue(COLUMN_WEB,contactNew.getHomePage())
                        .withValue(COLUMN_WEB_TYPE, contactNew.getHomePage())
                        .build());
    
                // postal
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?",
                                new String[]{id, MIMETYPE_STRING_ADDRESS})
                        .withValue(COLUMN_POSTAL,contactNew.getAddress())
                        .withValue(COLUMN_POSTAL_TYPE, contactNew.getAddressType())
                        .build());
    
                //update number
                if(!contactNew.getNumber().trim().equals("")) {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=? AND " + COLUMN_NUMBER_TYPE + "=?",
                                    new String[]{id, MIMETYPE_STRING_PHONE,contactNew.getNumberType()})
                            .withValue(COLUMN_NUMBER, contactNew.getNumber())
                            .withValue(COLUMN_NUMBER_TYPE, contactNew.getNumberType())
                            .build());
                    Log.d(TAG, "update number: " + contactNew.getNumber());
                }
    
                // fax
                if(!contactNew.getNumber().trim().equals("")) {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=? AND " + COLUMN_NUMBER_TYPE + "=?",
                                    new String[]{id, MIMETYPE_STRING_PHONE,contactNew.getFaxType()})
                            .withValue(COLUMN_NUMBER, contactNew.getFax())
                            .withValue(COLUMN_NUMBER_TYPE, contactNew.getFaxType())
                            .build());
                    Log.d(TAG, "update fax: " + contactNew.getNumber());
                }
    
    
                //update email if mail
                if(!contactNew.getEmail().trim().equals("")) {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?",
                                    new String[]{id, MIMETYPE_STRING_EMAIL})
                            .withValue(COLUMN_EMAIL, contactNew.getEmail())
                            .withValue(COLUMN_EMAIL_TYPE, contactNew.getEmailType())
                            .build());
                    Log.d(TAG, "update email: " + contactNew.getEmail());
                }
    
                try {
                    contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
                    Log.d(TAG, "update success");
                } catch (Exception e) {
                    Log.d(TAG, "update failed");
                    Log.e(TAG, e.getMessage());
                }
            }
            Log.w(TAG, "**update end**");
        }
  • 相关阅读:
    vscode配置备份
    正则替换html代码中img标签的src值
    使用css3原生变量实现主题换肤
    vue3基础知识学习系列(二)响应式原理实现
    vue3基础知识学习系列(一)api使用
    eslint规则中文解释
    开发笔记:使用canvas实现3D金字塔比例图表&#128200;
    实用网页UI调试技巧
    2020年特殊的一年,成就特殊的我们
    java设计模式之责任链模式
  • 原文地址:https://www.cnblogs.com/buakaw/p/8966121.html
Copyright © 2020-2023  润新知