【发布时间】:2015-05-05 13:54:50
【问题描述】:
在向内容提供者添加联系人时遇到一些问题,该联系人的号码以国家/地区代码为前缀,例如“+48515000111”。添加这种号码会导致我的联系人的号码类似于“48515000111”,因此删除了“+”字符。
这是我添加联系人的方式:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null).build());
//Phone Number
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, number)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.TYPE, "1").build());
//Display name/Contact name
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
try {
ctx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return "ok";
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error:"+e.getClass().getName()+":"+e.getMessage();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error:"+e.getClass().getName()+":"+e.getMessage();
}
有什么解决办法吗?
【问题讨论】:
-
不应该
Phone.TYPE是像TYPE_HOME这样的整数吗? developer.android.com/reference/android/provider/… -
嘿@shkschneider,感谢您的帮助,但问题出在其他地方:) 上面粘贴的代码没问题,但联系信息是通过 GCM 以 JSON 格式发送的。似乎将“+”放在通过 GCM 发送的 JSON 字符串中已被空格替换。在输入 JSON 之前,我必须对电话号码进行 URL 编码,现在我在
number变量中有我的“+”。 -
您应该将解决方案发布为答案(是的,回答您自己的问题)以帮助他人并关闭此(尚未解决的)问题。谢谢。
标签: android android-contentprovider android-contacts