android - 如何将电话号码、电子邮件、网站等添加到现有联系人

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

我无法更新我的联系人。

最佳答案

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

这可能有帮助

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

关于android - 如何将电话号码、电子邮件、网站等添加到现有联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10796640/

相关文章:

android - 当为给定的 URI 调用 ContentResolver.notifyChange() 时,是否会通知观察该 URI 的后代 URI 的 ContentObservers?

安卓 : my android app is not sending request header

android水平 ScrollView

android - 用于在 android 中列出元素的 xPath(使用 appium 自动化)

android - AAPT : error: attribute fastforward_increment (aka aplicationid. dev:fastforward_increment) 未找到

android - 蜂窝中的方向变化

java - 解码yuv420sp单像素版本

android - 如何更改工具栏主页图标颜色

java - 是否必须消耗自动续订的 android 应用内订阅?

java - 即使在我的应用程序为 "interact"之后,DDMS 中我的应用程序进程怎么可能为 "exit"?