• Android 联系人添加


    /**
         * @brief 新建联系人显示编辑界面
         * */
        public void insertContact(Contact contact,Context context) {
    
            if (context == null) {
                return;
            }
    
            Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
            intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
    
            intent.putExtra(ContactsContract.Intents.Insert.NAME, contact.getName());
    
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, contact.getNumber());
            intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, contact.getFax());
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);
    
            intent.putExtra(ContactsContract.Intents.Insert.EMAIL, contact.getEmail());
            intent.putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
    
            intent.putExtra(ContactsContract.Intents.Insert.POSTAL, contact.getAddress());
            intent.putExtra(ContactsContract.Intents.Insert.POSTAL_TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK);
    
            // Insert中没有的类型,通过Data写入
            ArrayList<ContentValues> data = new ArrayList<ContentValues>();
    
            ContentValues row1 = new ContentValues();
            row1.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
            row1.put(ContactsContract.CommonDataKinds.Website.DATA, contact.getHomePage());
            data.add(row1);
    
            ContentValues row2 = new ContentValues();
            row2.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
            row2.put(ContactsContract.CommonDataKinds.Photo.PHOTO, contact.getPhotoData());
            data.add(row2);
    
    //        Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
    
            intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
    
            context.startActivity(intent);
    
        }
    
        /**
         * 添加联系人,但是不显示编辑界面。
         * */
        private void addContact(Contact contact) {
    
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    
            ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                    .build());
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_NAME)
                    .withValue(COLUMN_NAME, contact.getName())
                    .build());
            Log.d(TAG, "add name: " + contact.getName());
    
            // photo
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_PHOTO)
                    .withValue(COLUMN_PHOTO, contact.getPhotoData())
                    .build());
    
            // web
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_WEB)
                    .withValue(COLUMN_WEB,contact.getHomePage())
                    .withValue(COLUMN_WEB_TYPE, contact.getHomePage())
                    .build());
    
            // postal
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_ADDRESS)
                    .withValue(COLUMN_POSTAL,contact.getAddress())
                    .withValue(COLUMN_POSTAL_TYPE, contact.getAddressType())
                    .build());
    
            if(!contact.getNumber().trim().equals("")) {
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                        .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_PHONE)
                        .withValue(COLUMN_NUMBER, contact.getNumber())
                        .withValue(COLUMN_NUMBER_TYPE, contact.getNumberType())
                        .build());
                Log.d(TAG, "add number: " + contact.getNumber());
            }
    
            if(!contact.getFax().trim().equals("")) {
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                        .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_PHONE)
                        .withValue(COLUMN_NUMBER, contact.getFax())
                        .withValue(COLUMN_NUMBER_TYPE, contact.getFaxType())
                        .build());
                Log.d(TAG, "add fax: " + contact.getFax());
            }
    
            if(!contact.getEmail().trim().equals("")) {
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                        .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_EMAIL)
                        .withValue(COLUMN_EMAIL, contact.getEmail())
                        .withValue(COLUMN_EMAIL_TYPE, contact.getEmailType())
                        .build());
                Log.d(TAG, "add email: " + contact.getEmail());
            }
    
            try {
                contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
                Log.d(TAG, "add contact success.");
            } catch (Exception e) {
                Log.d(TAG, "add contact failed.");
                Log.e(TAG, e.getMessage());
            }
        }
  • 相关阅读:
    iOS获取设备UUID和IDFA
    iOS之Xcode提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package
    iOS 打测试包给测试人员测试,两种安装方式
    ios APP进程杀死之后和APP在后台接收到推送点击跳转到任意界面处理
    iOS中统计平台的使用
    Android APK反编译
    Linux客户端、服务器、窗口管理器的关系
    Linux中ctrl-c, ctrl-z, ctrl-d 区别
    如何下载百度网盘已失效资源链接
    svn 批量更新 bat脚本
  • 原文地址:https://www.cnblogs.com/buakaw/p/8966086.html
Copyright © 2020-2023  润新知