如何预先填充“添加联系人”? Android 2.1 中带有姓氏字段的活动?

发布于 2024-12-15 22:20:53 字数 712 浏览 2 评论 0原文

我想显示预先填充了姓氏(也称为“姓氏”和“姓氏”)的“添加联系人”活动。目前我只能让它填充名字。这是我的代码:

Intent intentAddContact = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);

intentAddContact.putExtra(ContactsContract.Intents.Insert.NAME, "Mickey Mouse");
intentAddContact.putExtra(ContactsContract.Intents.Insert.PHONE,"01234567891");
intentAddContact.putExtra(ContactsContract.Intents.Insert.EMAIL, "[email protected]");

startActivityForResult(intentAddContact, ADD_CONTACT_REQUEST);

这会将“米老鼠”放在名字字段中。我需要在名字中添加“Mickey”,在姓氏中添加“Mouse”。我的应用程序需要在 Android 2.1(API 级别 7)上运行。

I would like to show the 'Add contact' activity prepopulated with a last name (also known as "family name" and "surname"). Currently I can only get it to populate the first name. Here's my code:

Intent intentAddContact = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);

intentAddContact.putExtra(ContactsContract.Intents.Insert.NAME, "Mickey Mouse");
intentAddContact.putExtra(ContactsContract.Intents.Insert.PHONE,"01234567891");
intentAddContact.putExtra(ContactsContract.Intents.Insert.EMAIL, "[email protected]");

startActivityForResult(intentAddContact, ADD_CONTACT_REQUEST);

This puts "Mickey Mouse" in the first name field. I need "Mickey" to go in the first name and "Mouse" to go in the last name. My app needs to run on Android 2.1 (API level 7).

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

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

发布评论

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

评论(1

优雅的叶子 2024-12-22 22:20:53

不幸的是,AOSP 中的库存“添加联系人”活动似乎仅支持提供全名(请参阅源代码 EditContactActivity.createContact()EntityModifier.parseExtras())。

近似您想要的一种方法是将联系人信息直接插入提供程序,然后启动“编辑联系人”活动,如下所示:

private void enlistMickey() throws RemoteException, OperationApplicationException {
    final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation.Builder builder;

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

    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
    builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(StructuredName.GIVEN_NAME, "Mickey");
    builder.withValue(StructuredName.FAMILY_NAME, "Mouse");
    ops.add(builder.build());

    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
    builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);
    builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    builder.withValue(Phone.NUMBER, "01234567891");
    ops.add(builder.build());

    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
    builder.withValueBackReference(Email.RAW_CONTACT_ID, 0);
    builder.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
    builder.withValue(Email.DATA, "[email protected]");
    ops.add(builder.build());

    final ContentResolver cr = getContentResolver();
    final ContentProviderResult[] res = cr.applyBatch(ContactsContract.AUTHORITY, ops);
    final Uri uri = ContactsContract.RawContacts.getContactLookupUri(cr, res[0].uri);
    final Intent intent = new Intent();
    intent.setAction(Intent.ACTION_EDIT);
    intent.setData(uri);
    startActivityForResult(intent, REQUEST_CODE);
}

与“添加”方法相比,使用此“插入和编辑”机制的一个显着区别是,如果已经存在具有匹配数据的现有联系人,则提供程序中的聚合过程将更有可能阻止我们创建新联系人。

Unfortunately, it seems that the stock "Add Contact" activity in AOSP only support the full name to be supplied (see source code for EditContactActivity.createContact() and EntityModifier.parseExtras()).

One way to approximate what you want is to insert the contact information into the provider directly and then launch the "Edit Contact" activity as follow:

private void enlistMickey() throws RemoteException, OperationApplicationException {
    final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation.Builder builder;

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

    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
    builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(StructuredName.GIVEN_NAME, "Mickey");
    builder.withValue(StructuredName.FAMILY_NAME, "Mouse");
    ops.add(builder.build());

    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
    builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);
    builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    builder.withValue(Phone.NUMBER, "01234567891");
    ops.add(builder.build());

    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
    builder.withValueBackReference(Email.RAW_CONTACT_ID, 0);
    builder.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
    builder.withValue(Email.DATA, "[email protected]");
    ops.add(builder.build());

    final ContentResolver cr = getContentResolver();
    final ContentProviderResult[] res = cr.applyBatch(ContactsContract.AUTHORITY, ops);
    final Uri uri = ContactsContract.RawContacts.getContactLookupUri(cr, res[0].uri);
    final Intent intent = new Intent();
    intent.setAction(Intent.ACTION_EDIT);
    intent.setData(uri);
    startActivityForResult(intent, REQUEST_CODE);
}

One notable difference using this "insert and edit" mechanism compared to the "add" method is that the aggregation process in the provider will be more likely to prevent us from creating a new contact if an existing one with matching data already existed.

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