如何在安卓系统中启动“添加联系人”活动

38

请问如何在Android中启动“添加联系人”活动?

谢谢。

8个回答

61

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);

1
这对我来说几乎可以工作,但我的onActivityResult方法没有被调用(因为据我所知,没有addActivityListener方法)。 - Akeem
在onActivityResult中,如何获取返回的意图中添加的名称和号码? - nmvictor
我更喜欢使用ACTION_INSERT_OR_EDIT而不是ACTION_INSERT - Alecs
只需在先前的源代码中将ACTION_INSERT更改为ACTION_INSERT_OR_EDIT即可。 - Alecs

46

这两行代码就能解决问题:

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

5
哈哈,我不知道为什么这不是被接受的答案。这就是你真正需要的。 - Laith Alnagem

16

这应该就是你在寻找的代码片段:

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)

10

这篇文章 可能会帮助你,或者至少指引你正确的方向。

希望对你有所帮助。

更新 05/11/2015:

根据评论,可以查看vickey的答案以获取适当的解决方案。


不,我正在寻找如何通过编程启动Android的添加联系人活动。我尝试了这个和删除已注释的代码,但两者都无效。 Intent intent = new Intent("android.intent.action.INSERT"); //intent.setType("vnd.android.cursor.item/person"); startActivity(intent); - hap497
这个答案真的很老了,下面是正确的代码:Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI); startActivity(intent); - Laith Alnagem
不能只用链接回答,看起来不像是个答案。 - Vladyslav Matviienko

10

如果你有一个电话号码,想要在你自己的应用程序中保存它,那么使用这段代码,它将会把你带到默认联系人应用的“创建联系人”页面。

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

8

我也尝试过这个。我能够在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);

希望这能有所帮助。

我尝试了这段代码,但它没有跳过确认对话框! - mahdi

5

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

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

请查看Android的源代码DialpadFragment.java中的getAddToContactIntent(String digits)方法。

但是由于api 10中People类已被弃用,你可以使用下面这行代码替代:

intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);

1
Intent localintent = new Intent("android.intent.action.INSERT",ContactsContract.Contacts.CONTENT_URI);
localintent.putExtra("phone", phoneNumber);
startActivity(localintent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接