Android:将号码添加到具有相同号码类型的现有联系人

时间:2015-12-21 23:29:17

标签: android android-contacts

我尝试使用相同的Phone.TYPE以编程方式向现有的Android联系人添加新号码。但是我的代码替换了给定Phone.TYPE中的现有手机号码(如果有的话)。

EG。如果12345678类别存储在Mobile类别下,我可以在同一类别87654321Mobile的应用中添加另一个号码People。但是,运行以下代码后,87654321会替换12345678。存储这两个数字的方式是什么。问题是,我不知道哪个Phone.TYPE为空以存储新号码。这就是我使用默认类型的原因。以下是我的代码。

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

String where1 = ContactsContract.CommonDataKinds.Phone.NUMBER + " = ? ";
String[] params1 = new String[] { oldNumber };
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(where1, params1)
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());

try
{
     cr.applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (RemoteException e)
{
     // TODO Auto-generated catch block
     e.printStackTrace();
}
catch (OperationApplicationException e)
{
     // TODO Auto-generated catch block
     e.printStackTrace();
}

2 个答案:

答案 0 :(得分:0)

在您进行更新而不是新插入时,会替换该号码。新数字应该是新的插入操作,因为您必须在数据表中创建新条目。为清楚起见,您可以提示用户输入数字类型或使用MOBILE类型插入,并让用户稍后编辑联系人。

// Inserts the specified phone number and type as a Phone data row
op =
        ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)

        .withValue(ContactsContract.Data.RAW_CONTACT_ID, raw_contact_id)

        // Sets the data row's MIME type to Phone
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)

        // Sets the phone number and type
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType);

// Builds the operation and adds it to the array of operations
ops.add(op.build());

答案 1 :(得分:0)

public static String getRawContactId(String contactId)
{
    String res = "";
    Uri uri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = new String[]{ContactsContract.RawContacts._ID};
    String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[]{ contactId };
    Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if(c != null && c.moveToFirst())
    {
        res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
        c.close();
    }

    return res;
}
//--------------------------------------------------
public static boolean addContactNumber(String contactId, String newNumber,@Nullable Object type)
{
    try
    {
        if (contactId == null || newNumber == null)
            return false;

        if(type == null)
            type = Phone.TYPE_WORK;

        ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<>();

        android.content.ContentProviderOperation.Builder t ;
        android.content.ContentProviderOperation b ;
        t = android.content.ContentProviderOperation.newInsert(Data.CONTENT_URI);
        t = t.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId));
        t = t.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        t = t.withValue(Phone.NUMBER, newNumber);
        t = t.withValue(Phone.TYPE, type);
        b = t.build();
        ops.add(b);

        ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        return true;
    }
    catch (Exception e) {}
    return false;
}
//--------------------------------------------------
public static boolean updateContactNumber(String contactId, String oldNumber, String newNumber,@Nullable Object type)
{
    try
    {
        if (contactId == null || oldNumber == null)
            return false;

        if(type == null)
            type = Phone.TYPE_WORK;

        String where = ContactsContract.Data.CONTACT_ID + " = ? AND "
                + Phone.NUMBER + " = ? AND "
                + ContactsContract.Data.MIMETYPE + " = ?";
        String[] numberParams = new String[]{contactId, oldNumber, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};

        ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<>();

        android.content.ContentProviderOperation.Builder t ;
        android.content.ContentProviderOperation b ;
        t = android.content.ContentProviderOperation.newUpdate(Data.CONTENT_URI);
        t = t.withSelection(where, numberParams);
        t = t.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber);
        t = t.withValue(Phone.TYPE, type);
        b = t.build();
        ops.add(b);

        ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        return true;
    }
    catch (Exception e) {}
    return false;
}
//--------------------------------------------------

示例:     addContactNumber(“313”,“+ 989139277303”,null);

添加+989139277303号码以联络