如何在android中添加联系人到手机联系人列表?

时间:2011-06-08 04:11:49

标签: android contacts

  

可能重复:
  How to add new contacts in android

我想知道如何将数据保存到我的Android手机的联系人列表中。数据包含姓名,图片,phoneno,emailid,dob,地址等。如何将这些所有信息添加到我的手机联系人列表中。< / p>

请帮助我..

1 个答案:

答案 0 :(得分:0)

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE, null)
   .withValue(RawContacts.ACCOUNT_NAME, null)
   .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
   .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
   .build());

    Log.d("test","2222222222");

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
   .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, parser.getText())
   .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_WORK)
   .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
   .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 3)
   .build());

Log.d("test","333333");

try {                                                                    
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    Log.d("test","4444");
} catch (Exception e) {
    Log.d("test","5555");
    // Display warning
    Context ctx = getApplicationContext();
    CharSequence txt = "contactCreationFailure";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(ctx, txt, duration);
    toast.show();

    // Log exception
    Log.e("test", "Exceptoin encoutered while inserting contact: " + e);
}