如何以编程方式添加联系人?

发布于 2024-09-19 03:18:16 字数 2250 浏览 9 评论 0原文

我正在尝试以编程方式在 Android 中添加联系人。这是我的代码:

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,"[email protected]") .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();
}

我这样做正确吗?如何添加帐户名称和帐户类型?

注意:我没有创建任何帐户。是否必须拥有帐户才能添加联系人?

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,"[email protected]") .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?

NOTE: I don't have any account created. Is it mandatory to have account in order to add a contact?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-09-26 03:18:16

联系人 API 将联系人存储为隐形接触:如何使其可见?检查这个我希望它会有用!

无需在手机中创建任何帐户!...我认为他们是一些默认帐户来做到这一点!

Contact API storing contact as an 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文