java 在Android上添加新联系人

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 在Android上添加新联系人相关的知识,希望对你有一定的参考价值。

private void addContact(ContactDetail c) {
		 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		  int rawContactInsertIndex = ops.size();

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

		  //Phone Number
		  if(null!=c.getPhoneNumber()){
		  ops.add(ContentProviderOperation
		    .newInsert(ContactsContract.Data.CONTENT_URI)
		    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
		      rawContactInsertIndex)
		    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
		    .withValue(Phone.NUMBER, c.getPhoneNumber())
		    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
		    .withValue(Phone.TYPE, "1").build());
		  }
		  
		  //Display name/Contact name
		  ops.add(ContentProviderOperation
		    .newInsert(ContactsContract.Data.CONTENT_URI)
		    .withValueBackReference(Data.RAW_CONTACT_ID,
		      rawContactInsertIndex)
		    .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
		    .withValue(StructuredName.DISPLAY_NAME, c.getName())
		    .build());
		  //Email details
		  if(c.getEmail()!=null){
		  ops.add(ContentProviderOperation
		    .newInsert(ContactsContract.Data.CONTENT_URI)
		    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
		      rawContactInsertIndex)
		    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
		    .withValue(ContactsContract.CommonDataKinds.Email.DATA, c.getEmail())
		    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
		    .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "2").build());

		  }
		  //Postal Address

		    ops.add(ContentProviderOperation
		      .newInsert(ContactsContract.Data.CONTENT_URI)
		      .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
		        rawContactInsertIndex)
		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POBOX, "Postbox")

		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street")

		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city")

		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, "region")

		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode")

		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country")

		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, "3")


		      .build());


		  //Organization details
		  ops.add(ContentProviderOperation
		    .newInsert(ContactsContract.Data.CONTENT_URI)
		    .withValueBackReference(Data.RAW_CONTACT_ID,
		      rawContactInsertIndex)
		    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
		    .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, "Devindia")
		    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
		    .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, "Developer")
		    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
		    .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, "0")

		    .build());
		  //IM details
		    ops.add(ContentProviderOperation
		      .newInsert(ContactsContract.Data.CONTENT_URI)
		      .withValueBackReference(Data.RAW_CONTACT_ID,
		        rawContactInsertIndex)
		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
		      .withValue(ContactsContract.CommonDataKinds.Im.DATA, "ImName")
		      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE )
		      .withValue(ContactsContract.CommonDataKinds.Im.DATA5, "2")


		      .build());
		  try {
		   ContentProviderResult[] res = getContentResolver().applyBatch(
		     ContactsContract.AUTHORITY, ops);
		  } catch (RemoteException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		  } catch (OperationApplicationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		  }

		 }

以上是关于java 在Android上添加新联系人的主要内容,如果未能解决你的问题,请参考以下文章

Android - 新数据记录添加到错误的联系人

如何删除以编程方式添加新联系人时添加的 Android 联系人应用程序中的重复条目?

如何告诉应用程序在地址簿中添加或删除或修改了新联系人及其在android中的详细信息

如何在没有任何服务器端代码的情况下借助 HTML 在 android 和 iphone 中添加新的联系人屏幕?

无法更新或添加新的联系人照片 Android

“添加联系人”activity.xml 文件在 Android Studio 中的格式不正确