我在手机中有一个联系人,例如 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());}