如何在 android 中启动“添加联系人”活动

Posted

技术标签:

【中文标题】如何在 android 中启动“添加联系人”活动【英文标题】:How can I launch the 'Add Contact' activity in android 【发布时间】:2010-12-26 02:19:18 【问题描述】:

您能告诉我吗? 谢谢。

【问题讨论】:

【参考方案1】:

API Level 5 及以上解决方案

// Add listener so your activity gets called back upon completion of action,
// in this case with ability to get handle to newly added contact
myActivity.addActivityListener(someActivityListener);

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

// Just two examples of information you can send to pre-fill out data for the
// user.  See android.provider.ContactsContract.Intents.Insert for the complete
// list.
intent.putExtra(ContactsContract.Intents.Insert.NAME, "some Contact Name");
intent.putExtra(ContactsContract.Intents.Insert.PHONE, "some Phone Number");

// Send with it a unique request code, so when you get called back, you can
// check to make sure it is from the intent you launched (ideally should be
// some public static final so receiver can check against it)
int PICK_CONTACT = 100;
myActivity.startActivityForResult(intent, PICK_CONTACT);

【讨论】:

这几乎对我有用,但我的 onActivityResult 方法没有被调用(此响应中的第一行被省略,因为据我所知没有方法 addActivityListener) 如何取回onActivityResult返回的intent中添加的姓名和号码? 我更喜欢使用 ACTION_INSERT_OR_EDIT 而不是 ACTION_INSERT 只要把之前源码中的ACTION_INSERT改成ACTION_INSERT_OR_EDIT就可以了【参考方案2】:

这两行可以解决问题:

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

【讨论】:

大声笑,我不知道为什么这不是公认的答案。这就是你真正需要的。【参考方案3】:

这应该是您正在寻找的 sn-p:

Intent addContactIntent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
addContactIntent.putExtra(Contacts.Intents.Insert.NAME, "Jean-Claude"); // an example, there is other data available
startActivity(addContactIntent)

【讨论】:

【参考方案4】:

This 的帖子可能会帮助您或至少为您指明正确的方向。

希望这会有所帮助。

2015 年 5 月 11 日更新:

根据 cmets,请查看 vickey's answer below 以获得适当的解决方案。

【讨论】:

没有。我正在寻找如何以编程方式启动 Android 的 Add Contact Activiy。我尝试了这两个并删除了注释代码,两者都不起作用。 Intent 意图 = new Intent("android.intent.action.INSERT"); //intent.setType("vnd.android.cursor.item/person");开始活动(意图); 这个答案真的很老了,这里是正确的代码,如下所述: Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);开始活动(意图); 看起来不像是一个答案。你不能只用其他答案的链接来回答。【参考方案5】:

如果您有电话号码并想将其保存在您自己的应用程序中,那么使用此代码会将您转到默认联系人应用程序的“创建联系人”页面。

  Intent addContactIntent = new Intent(Intent.ACTION_INSERT);
  addContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE,phnno.getText().toString());
  startActivity(addContactIntent);

【讨论】:

【参考方案6】:

我也试图这样做。我能够使用 Android 2.2 启动活动。不过,我还没有尝试在其他 SDK 版本中使用/测试它。

Intent intent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, Uri.parse("tel:" + currentNum.getText())); //currentNum is my TextView, you can replace it with the number directly such as Uri.parse("tel:1293827")
intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true); //skips the dialog box that asks the user to confirm creation of contacts
startActivity(intent);

希望这可能会有所帮助。

【讨论】:

我尝试了这段代码,但它没有跳过确认对话框!【参考方案7】:

如果您需要将电话号码添加到现有联系人或创建新联系人(就像本机拨号器使用新电话号码一样),此代码可能正是您所需要的:

final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.putExtra(Insert.PHONE, digits);
intent.setType(People.CONTENT_ITEM_TYPE);
startActivity(intent);

看看Android的source code中的DialpadFragment类,搜索方法getAddToContactIntent(String digits)

但由于 People 类在 api 10 中已弃用,您可能希望改用这一行:

intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);

【讨论】:

【参考方案8】:
Intent localintent = new Intent("android.intent.action.INSERT",ContactsContract.Contacts.CONTENT_URI);
localintent.putExtra("phone", phoneNumber);
startActivity(localintent);

【讨论】:

以上是关于如何在 android 中启动“添加联系人”活动的主要内容,如果未能解决你的问题,请参考以下文章

Kivy 使用预填充的联系人字段启动 android 意图

如何在android中以编程方式将铃声添加到本机应用程序联系人

android怎么 添加联系人到手机

从联系人意图添加联系人后如何刷新列表视图?

如何通过Android手机联系人列表中的代码添加联系人详细信息..?

Android SQLite“动态”添加联系人