由网友(超级凶嗷呜)分享简介:我试图以编程方式添加在Android中的联系人。这是我的code:I am trying to programmatically add a contact in Android. Here is my code:ArrayList ops = new Array...
我试图以编程方式添加在Android中的联系人。这是我的code:
I am trying to programmatically add a contact in Android. Here is my code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,"Google")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,"user1@gmail.com") .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract
.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, emailType)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Context ctx = getApplicationContext();
CharSequence txt = "Contact " + name +" added successfully";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(ctx, txt, duration);
toast.show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
}
我是否在做正确吗?我如何添加帐户名和帐户类型?
Am I doing this correctly? How can I add an account name and account type?
注:我没有创建的任何帐户。是否必须拥有帐户才能添加联系人?
推荐答案
contact API存储联系人作为一种无形的contact.how使其可见?检查这一点,我希望这将是有益的!
contact api storing contact as a invisible contact.how to make it visible? check this i hope it will be useful!
没有必要在手机上创建任何帐号!......我认为他们是一些默认的帐户要做到这一点!
there is no need of creating any account in the phone!... i think their is some default account to do that!
相关推荐
最新文章