【发布时间】:2010-12-26 02:19:18
【问题描述】:
您能告诉我如何在 android 中启动“添加联系人”活动吗? 谢谢。
【问题讨论】:
标签: android
您能告诉我如何在 android 中启动“添加联系人”活动吗? 谢谢。
【问题讨论】:
标签: android
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);
【讨论】:
这两行可以解决问题:
Intent intent = new Intent(Intent.ACTION_INSERT,
ContactsContract.Contacts.CONTENT_URI);
startActivity(intent);
【讨论】:
这应该是您正在寻找的 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)
【讨论】:
This 的帖子可能会帮助您或至少为您指明正确的方向。
希望这会有所帮助。
根据 cmets,请查看 vickey's answer below 以获得适当的解决方案。
【讨论】:
如果您有电话号码并想将其保存在您自己的应用程序中,那么使用此代码会将您转到默认联系人应用程序的“创建联系人”页面。
Intent addContactIntent = new Intent(Intent.ACTION_INSERT);
addContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE,phnno.getText().toString());
startActivity(addContactIntent);
【讨论】:
我也试图这样做。我能够使用 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);
希望这可能会有所帮助。
【讨论】:
如果您需要将电话号码添加到现有联系人或创建新联系人(就像本机拨号器使用新电话号码一样),此代码可能正是您所需要的:
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);
【讨论】:
Intent localintent = new Intent("android.intent.action.INSERT",ContactsContract.Contacts.CONTENT_URI);
localintent.putExtra("phone", phoneNumber);
startActivity(localintent);
【讨论】: