[英]How to Delete phone number from contact in android?
我想從android數據庫中刪除(例如手機號碼)。 為此,我傳遞的查詢如下
public class ContactDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String number = "2222";
Long id = getID(number);
int i = getContentResolver().delete(RawContacts.CONTENT_URI, RawContacts._ID+"=?", new String[]{id.toString()});
System.out.println("Deleted"+i);
}
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;
}
}
但它正在刪除整個聯系人。
我應該使用什么來刪除該電話號碼(不是整個聯系人)?
您使用ContentResolver的刪除方法,以便刪除整個聯系人。 要將此聯系人的電話號碼更新為空值,您需要使用ContactsContact API。
http://developer.android.com/reference/android/provider/ContactsContract.Data.html
通過提供聯系人原始ID和Phone.CONTENT_ITEM_TYPE,您只能請求屬於此聯系人的電話號碼,然后刪除所有這些電話號碼。
ArrayList<ContentProviderOperation> ops = Lists.newArrayList();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data._ID + "=? and " + Data.MIMETYPE + "=?", new String[]{String.valueOf(contactId), Phone.CONTENT_ITEM_TYPE})
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.