1

在为不同电话类型的联系人更新电话号码时,我遇到了一些问题。我需要为所有数字添加前缀 xxx。例如:“保罗”有手机:42342397908,家庭:3453459534,工作:4345436533,其他:3253454354,其他:3465465464,手机:34564654654。更新正在运行,但保存时是这样的。手机:xxx42342397908,家庭:xxx3453459534,工作:xxx4345436533,其他:xx3253454354,其他:xx3253454354,手机:xxx42342397908。它覆盖具有相同电话类型和相同电话号码的电话。请在下面查看我的更新功能并帮助我。

 private void updateContact(String name, String phone,String id, String type) {
        ContentResolver cr = getContentResolver();

        String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
                    ContactsContract.Data.MIMETYPE + " = ? AND " +
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? ";

        String[] params = null;
        /*
        ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?",  
                        new String[]{String.valueOf(id), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
        */   
        if (Integer.valueOf(type) == Phone.TYPE_MOBILE)
        {
        params = new String[] {name,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
        }
        else if (Integer.valueOf(type) == Phone.TYPE_HOME)
        {
            params = new String[] {name,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)};
            }
        else if (Integer.valueOf(type) == Phone.TYPE_WORK)
        {
            params = new String[] {name,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
            }
        else if (Integer.valueOf(type) == Phone.TYPE_OTHER)
        {
            params = new String[] {name,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER)};
            }
      //  ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
        Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI, null, where, params, null);
      /* */



        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        if ( (null == phoneCur) ) {
            createContact(name, phone);
        } else {
            ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                    .withSelection(where, params)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
                    .build());

        }

        phoneCur.close();
        try {
            cr.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
4

0 回答 0