开发者

How to delete a contact in android 2.1

开发者 https://www.devze.com 2022-12-24 13:25 出处:网络
I have added one contact to android by following code. ContentValues values = new ContentValues(); Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);

I have added one contact to android by following code.

 ContentValues values = new ContentValues();
 Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
 long rawContactId = Cont开发者_开发技巧entUris.parseId(rawContactUri);

 values.clear();
 values.put(Data.RAW_CONTACT_ID, rawContactId);
 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
 values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
 getContentResolver().insert(Data.CONTENT_URI, values);

It shows up on emulator 2.1, but when i am going to delete it manually by "delete contact" option, its not deleteing from emulator.

If I edit some thing on it then only it deletes.

How can i directly delete it from menu ?

Thanks in advance...


Could you please use the below code to add the contact. This will not affect your emulator to delete contact from menu without editing the same.

import android.provider.Contacts.People; 

public void addvaluestocontent()
{
    ContentValues values = new ContentValues();

    values.put(People.NAME, "Abraham Lincoln");
    values.put(People._ID, "1");
    values.put(People.NUMBER, "23333");

    Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
}


You have to save one more field, either "Given Name" or "Family Name". You can test it manually by saving contacts. First try to save manaully only number and then with saving contacts with both name and number.


Simple, Delete the .db file all contacts were deleted. and android will create new file automatically.

path for .db is: data/data/com.android.providers.contacts/database/contacts.db


Use this method to Check your SDK version and Get the Contacts content Uri. After, that you can insert the contacts with this new Content Uri,

static 
{
int sdk=new Integer(Build.VERSION.SDK).intValue();

if (sdk>=5) {
  try {
    Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");

    CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
  }
  catch (Throwable t) {
    Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
  }
}
else {
  CONTENT_URI=Contacts.People.CONTENT_URI;
}
}

Refer, CommonsWare Examples for Contacts Content Uri. This may be helps you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号