简体   繁体   English

android为联系人添加多个电话号码

[英]android add multiple phone numbers for a contact

using Android SDK > 5, I m creating a contact by starting the ACTION_INSERT activity. 使用Android SDK> 5,我通过启动ACTION_INSERT活动创建联系人。 I want to add several phone numbers (work, home, etc...) for the contact using the following code: 我想使用以下代码为联系人添加几个电话号码(工作,家庭等...):

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

for(ContactInfo.Phone p : phones)
      {

       newIntent.putExtra(ContactsContract.Intents.Insert.PHONE, p.number);
       newIntent.putExtra(ContactsContract.Intents.Insert.PHONE_ISPRIMARY, p.isPrimary ? new Integer(1) : null);
       newIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, unconvertPhoneType(p.type));

      }

(unconvertPhoneType() is a function to get the type as CommonDataKinds.Phone.TYPE_XXX) (unconvertPhoneType()是一个获取类型为CommonDataKinds.Phone.TYPE_XXX的函数)

I have just one example being inserted in the contact. 我只有一个插入联系人的例子。 What is wrong with the above? 上面有什么问题?

Additionally, in LogCat logs, I have also the following error: 此外,在LogCat日志中,我还有以下错误:

12-14 11:09:03.015: WARN/Bundle(1724): Key phone_type expected String but value was a java.lang.Integer. 12-14 11:09:03.015:WARN / Bundle(1724):键phone_type expected String但值是java.lang.Integer。 The default value was returned. 返回了默认值。

looks like it comes from PHONE_TYPE, however CommonDataKinds.Phone.TYPE_XXX is of type integer, so i am not sure... What is the cause of that? 看起来它来自PHONE_TYPE,但是CommonDataKinds.Phone.TYPE_XXX是整数类型,所以我不确定......原因是什么?

Thanks! 谢谢!

The logcat is telling you that it was expecting a String value, not an integer. logcat告诉你它期待一个String值,而不是一个整数。 That is your problem. 那是你的问题。 The phone number should be of type String. 电话号码应为String类型。

I pulled this example from the official documentation, and you can see the phone number is represented as a String with "". 我从官方文档中提取了这个示例,您可以看到电话号码表示为带有“”的字符串。

// Add a phone number for Abraham Lincoln.  Begin with the URI for
// the new  record     just returned by insert(); it ends with the _ID
// of the new record, so we don't have to add the ID ourselves.
// Then append the designation for the phone table to this URI,
// and use the resulting URI to insert the phone number.
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear(); 
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE); 
values.put(People.Phones.NUMBER, "1233214567");
getContentResolver().insert(phoneUri, values);

Hope this helps. 希望这可以帮助。

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

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