我正在通过以下方式将 jsonned 联系人添加到我的 android 设备中(请注意,下面的代码是草稿 - 也就是说它可能包含一些低效的内容);
...
if(!is_duplicated)
{
String firstName = p_contact.getContactFirstName();
String middleName = p_contact.getContactMiddleName();
String lastName = p_contact.getContactLastName();
String orgName = p_contact.getOrganizationName();
boolean isFirstNameNull = (firstName != null && firstName.equalsIgnoreCase("null"));
boolean isMiddleNameNull = (middleName != null && middleName.equalsIgnoreCase("null"));
boolean isLastNameNull = (lastName != null && lastName.equalsIgnoreCase("null"));
boolean isOrgNameNull = (orgName != null && orgName.equalsIgnoreCase("null"));
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//----------------- Names
if(!isFirstNameNull && !isMiddleNameNull && !isLastNameNull) // 111
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
.build());
}
else if(isFirstNameNull && !isMiddleNameNull && !isLastNameNull) // 011
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
.build());
}
else if(!isFirstNameNull && isMiddleNameNull && !isLastNameNull) // 101
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
.build());
}
else if(!isFirstNameNull && !isMiddleNameNull && isLastNameNull) // 110
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
.build());
}
else if(isFirstNameNull && isMiddleNameNull && !isLastNameNull) // 001
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
.build());
}
else if(isFirstNameNull && !isMiddleNameNull && isLastNameNull) // 010
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
.build());
}
else if(!isFirstNameNull && isMiddleNameNull && isLastNameNull) // 100
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
.build());
}
else if(isFirstNameNull && isMiddleNameNull && isLastNameNull) // 000
{
/*ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Anonim")
.build());*/
}
if(!isOrgNameNull)
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, pIndex)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, orgName)
.build());
}
Iterator<TreeMap.Entry<String, String>> entries = p_contact.getPhonesHashMap().entrySet().iterator();
while (entries.hasNext())
{
TreeMap.Entry<String, String> entry = entries.next();
String type = entry.getKey();
String number = entry.getValue();
if(type.equalsIgnoreCase("home"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
.build());
}
else if(type.equalsIgnoreCase("work"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());
}
else if(type.equalsIgnoreCase("mobile"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
}
else if(type.equalsIgnoreCase("faxhome"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME)
.build());
}
...
}
Iterator<TreeMap.Entry<String, String>> email_entries = p_contact.getEmailsHashMap().entrySet().iterator();
while (email_entries.hasNext())
{
TreeMap.Entry<String, String> entry = email_entries.next();
String type = entry.getKey();
String address = entry.getValue();
if(firstName != null && firstName.equals("Yasin"))
{
Log.d("ContactIssueHandler", "email is: " + address);
Log.d("ContactIssueHandler", "email type is: " + type);
}
if(type.equalsIgnoreCase("home"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, address)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_HOME)
.build());
}
else if(type.equalsIgnoreCase("work"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
}
else if(type.equalsIgnoreCase("mobile"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM)
.build());
}
else if(type.equalsIgnoreCase("other"))
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_OTHER)
.build());
}
else
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM)
.withValue(ContactsContract.CommonDataKinds.Email.LABEL, type)
.build());
}
}
// Asking the Contact provider to create a new contact
try
{
p_contact.setIsAdded(true);
p_contact.setReasonForFail(REASON_FOR_FAIL.NO_FAIL);
ContentProviderResult[] results = p_application_context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.d("ContactIssueHandler", "result: " + results.length);
Log.d("ContactIssueHandler", "opssize: " + ops.size());
if(results.length != ops.size())
{
p_contact.setIsAdded(false);
p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
//or throw exp.
}
}
catch (OperationApplicationException oaexp)
{
oaexp.printStackTrace();
p_contact.setIsAdded(false);
p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
}
catch (RemoteException rexp)
{
rexp.printStackTrace();
p_contact.setIsAdded(false);
p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
}
catch (Exception e)
{
e.printStackTrace();
p_contact.setIsAdded(false);
p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
}
...
无论如何..每次我添加一个 jsonned 联系人时,我都需要执行上面的代码。有趣的是 - 当我插入例如 50 个联系人时,总是不会插入一些特定的联系人,因为 ContentResolverResult 通过 applyBatch 方法返回一个空数组。(不是因为抛出异常)。但是如果我尝试一个一个添加相同的“无法添加”的联系人,它们就会被添加!
那么可能是什么问题呢?