与 Android 中的国家/地区代码建立联系

Creating contact with country code in Android

我在向 Content Provider 添加联系人时遇到了一些问题,号码以国家代码为前缀,例如“+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();
        }

有什么办法可以解决这个问题吗?

问题出在其他地方。上面粘贴的代码没问题,但联系信息是通过 GCM 以 JSON 格式发送的。似乎通过 GCM 发送的 JSON 字符串中的“+”被替换为白色 space。在放入 JSON 之前,我必须对 phone 数字进行 URL 编码,现在我在 number 变量中有了我的“+”。