简体   繁体   English

如何从android中删除联系人的电话号码?

[英]How to Delete phone number from contact in android?

I want to delete (eg mobile number) from android database. 我想从android数据库中删除(例如手机号码)。 For that I am passing query as follow 为此,我传递的查询如下

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;
    }    
}

but it is deleting entire contact. 但它正在删除整个联系人。

What should I use to delete only that phone number (not entire contact)? 我应该使用什么来删除该电话号码(不是整个联系人)?

You use the delete method of the ContentResolver so you delete the whole contact. 您使用ContentResolver的删除方法,以便删除整个联系人。 To update the phone number of this contact to an empty value, you need to use the ContactsContact API. 要将此联系人的电话号码更新为空值,您需要使用ContactsContact API。

http://developer.android.com/reference/android/provider/ContactsContract.Data.html http://developer.android.com/reference/android/provider/ContactsContract.Data.html

By providing the contact raw id and a Phone.CONTENT_ITEM_TYPE, you can request only for phone number belonging to this contact and then remove all of them. 通过提供联系人原始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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM