开发者

android add multiple phone numbers for a contact

using Android SDK > 5, I m creating a contact by starting the ACTION_INSERT activity. 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)

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:

12-14 11:09:03.015: WARN/Bundle(1724): Key phone_type expected String but value was a java.lang.Integer. The default value was returned.

looks like it comes from PHO开发者_JS百科NE_TYPE, however CommonDataKinds.Phone.TYPE_XXX is of type integer, so i am not sure... What is the cause of that?

Thanks!


The logcat is telling you that it was expecting a String value, not an integer. That is your problem. The phone number should be of type 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜