2

我在手机中有一个联系人,例如 name="ABC"。电话号码="123456789" type="work" google number="987654321" type="work"。现在当我更新号码“123456789”的联系人时,首先获取该联系人的ID,然后更新联系人phone.type="work"。但问题是,当我更新联系人时,联系人将同时更新电话号码和谷歌号码等号码。所以我怎样才能只更新手机的联系人号码而不更新任何其他帐户加入了这个id?。我写的代码如下:

    public Long getID(String number) {

            Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(number));
            Cursor c = getContentResolver().query(uri,
                    new String[] { PhoneLookup._ID}, null, null, null);
            while (c.moveToNext()) {
                return c.getLong(c.getColumnIndex(PhoneLookup._ID));
            }
            return null;
        }  
 public int gettype(String number) {

            Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(number));
            Cursor c = getContentResolver().query(uri,
                    new String[] { PhoneLookup.TYPE }, null, null, null);
            while (c.moveToNext()) {
            return c.getInt(c.getColumnIndex(PhoneLookup.TYPE));

            }
            return 0;
        }


Long id = getID(delnumber);
int contact_type= gettype(delnumber);


 String selectPhone = Data.CONTACT_ID+ "=? AND "    + Data.MIMETYPE+ "='"+ Phone.CONTENT_ITEM_TYPE+ "'" + " AND " + Phone.TYPE + "=?";
                                                    Log.i("type",""+contact_type);
                                                    if(contact_type==1)
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_HOME)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());}
                                                    else if(contact_type==2)
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_MOBILE)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());}
                                                    else if(contact_type==3)
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_WORK)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());}
                                                    else
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_MOBILE)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());} 
4

1 回答 1

0

首先,使用上面的格式很难阅读您的代码,这可能是为什么没有人试图回答这个问题,但这是我用来更新电话号码而不更新任何其他电话号码的代码:

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(ContactsContract.Data.CONTACT_ID + " = ?", new String[] {userId})
            .withSelection(ContactsContract.Data._ID + " = ?", new String[] {phoneId})
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.Data.DATA1, phoneNumber)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
            .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, label)
            .build());

    try 
    {
        resolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } 

关键是要在您的查询选择中包含联系人的联系人 ID 和要更新的电话的电话 ID。

于 2013-02-28T20:25:18.073 回答