如何在现有联系人中添加电话号码,电子邮件,网站等

时间:2012-05-29 09:44:09

标签: android

我正在开发一个应用程序,我只需点击一下按钮就可以将电话号码,电子邮件,网站,地址等添加到我现有的联系人中。

点击按钮的功能就在这里

private void updateContact(String name) 
   {
Log.d(TAG, "in updatecontact()");
Log.d(TAG,"Contact name to be updated = "+name);
ContentResolver cr = getContentResolver();
     String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
            ContactsContract.Data.MIMETYPE + " = ? AND " +
            String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? ";
String[] params = new String[] {name,
        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
        String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)};

Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI, null, where, params, null);

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

if ( (phoneCur == null)  ) {
    add_new_contact();
} else {
    // Phone no
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(where, params)
            .withValue(ContactsContract.CommonDataKinds.Phone.DATA, Tel)
            .build());
    // Email
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(where, params)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, Email)
            .build());
    // Website
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(where, params)
            .withValue(ContactsContract.CommonDataKinds.Website.DATA, Url)
            .build());
    //Organization
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(where, params)
            .withValue(ContactsContract.CommonDataKinds.Organization.DATA, Org)
            .build());
}

phoneCur.close();

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();
}}}

我无法更新我的联系人。

1 个答案:

答案 0 :(得分:1)

我假设你不知道该怎么做,那就是你的问题。

这可能会有所帮助

ContentResolver cResolver = context.getContentResolver();
public void AddToContact()
{
    insertContentValues(cResolver, Contacts.Phones.CONTENT_URI, getPhoneCV(phone));
}

public ContentValues getPhoneCV(RowData data) {
        ContentValues cv = new ContentValues();

        String PhoneNumber = "055434553";
        cv.put(Contacts.Phones.NUMBER,PhoneNumber );
        return cv;
    }

private Uri insertContentValues(ContentResolver cResolver, Uri uri, ContentValues cv) {
        if (cv != null) {
          return cResolver.insert(uri, cv);
        }
        return null;
    }