简体   繁体   English

Android使用Intents.Insert插入联系人不起作用

[英]Android insert contact with Intents.Insert not works

I'm trying to develop a app in this I need to access the contacts to add phone number with name. 我正在尝试开发一个应用程序,我需要访问联系人以添加带有姓名的电话号码。 for this I'm using following code. 为此,我正在使用以下代码。

Intent intent = new Intent(Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(Intents.Insert.NAME, "Silambarasan");
intent.putExtra(Intents.Insert.PHONE, 9876543210L);
intent.putExtra(Intents.Insert.PHONE_TYPE, Phone.TYPE_MOBILE);
startActivity(intent);

This code opens the add contact screen, but in the screen only name "Silambarasan" sent from this code. 该代码将打开“添加联系人”屏幕,但在该屏幕中,仅此代码发送的名称为“ Silambarasan”。 Phone "Number :9876543210L "is not passed. 电话“号码:9876543210L”未通过。

Do I use correct way? 我使用正确的方法吗?

Thanks in advance. 提前致谢。

Try this, 尝试这个,

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

                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.NAME, "name");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "phone");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, "email");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "address");

                                        startActivityForResult(addPersonIntent, CREATE_NEW);

From ContactsContract.Intents.Insert documentation ContactsContract.Intents.Insert文档

public static final String PHONE : Added in API level 5 public static final String PHONE :在API级别5中添加

The extra field for the contact phone number. 联系人电话号码的额外字段。

Type: String // Clears that you need to pass String value for this extra 类型: String //清除您需要传递String值的额外内容

Constant Value: "phone" 常数: “电话”


Simply pass number as String into extra that will work. 只需将数字作为String传递给将起作用的多余元素。

So change intent.putExtra(Intents.Insert.PHONE, 9876543210L); 因此更改 intent.putExtra(Intents.Insert.PHONE, 9876543210L);

with

 intent.putExtra(Intents.Insert.PHONE, "9876543210");

Why don't you just pass it like this? 你为什么不就这样通过它?

intent.putExtra("nameForThisExtra", "Silambarasan")
intent.putExtra("nameForThisExtra", "9876543210L");
intent.putExtra("nameForThisExtra", Phone.TYPE_MOBILE);

and call it in the ohter activity like this: 并在这样的其他活动中调用它:

String name = getIntent().getStringExtra("nameForThisExtra");
String number = getIntent().getStringExtra("nameForThisExtra");

That should work. 那应该工作。

The first parameter is the name you want to give this Extra, the second parameter the value. 第一个参数是你想给这个额外的,第二个参数的值的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM