如何更新现有的联系人? [英] How to update existing contact?
本文介绍了如何更新现有的联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个现有的联系人,我需要添加一个工作地址到现有的联系人。我现在用的是下面的code,但它不工作。
字符串selectPhone = Data.CONTACT_ID +=?AND+ Data.MIMETYPE +=+
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE +
'+和+ ContactsContract.CommonDataKinds.StructuredPostal.TYPE +?=;
的String [] phoneArgs =新的String []
{将String.valueOf(的ContactID),将String.valueOf(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone,phoneArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET,街道)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY,CITY)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION,区)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POST code,POST code)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,COUNTRY)
。建立());
。this.context.getContentResolver()applyBatch(ContactsContract.AUTHORITY,OPS);
这个解决的办法?
解决方案
/ **
* @参数名联系人的姓名
*参数号码的手机联系人数量
*参数电子邮件工作电子邮件联系地址
*接触的要更新@参数的ContactID ID
* @返回true,如果联系成功更新< BR />
*假,如果接触不会被更新< BR />
*假,如果电话号码包含的任何字符(它应该只包含数字)< BR />
*假,如果电子邮件地址无效< BR />< BR />
*
*您可以通过其中的3个参数的任何一个更新contact.Passing所有三个参数为< B>空< / B>不会更新联系人
*< BR />< BR />< B>注意:< / B>这方法要求的权限和LT; B> android.permission.WRITE_CONTACTS< / B>< BR />
* /
公共布尔updateContact(字符串名称,串号,串电子邮件,字符串的ContactID)
{
布尔成功= TRUE;
字符串phnumexp =^ [0-9] * $;
尝试
{
名称= name.trim();
电子邮件= email.trim();
数= number.trim();
如果(name.equals()及&安培; number.equals()及&安培; email.equals())
{
成功= FALSE;
}
否则如果((number.equals())及!&安培;!(匹配(号码,phnumexp)))
{
成功= FALSE;
}
否则如果((email.equals())及!&安培;!(isEmailValid(电子邮件)))
{
成功= FALSE;
}
其他
{
ContentResolver的ContentResolver的= activity.getContentResolver();
字符串,其中= ContactsContract.Data.CONTACT_ID +=和+ ContactsContract.Data.MIMETYPE +=;
的String [] emailParams =新的String [] {的ContactID,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
的String [] nameParams =新的String [] {的ContactID,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
的String [] numberParams =新的String [] {的ContactID,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
ArrayList的< android.content.ContentProviderOperation> OPS =新的ArrayList< android.content.ContentProviderOperation>();
如果(!email.equals())
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(其中,emailParams)
.withValue(Email.DATA,电子邮件)
。建立());
}
如果(!name.equals())
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(其中,nameParams)
.withValue(StructuredName.DISPLAY_NAME,名)
。建立());
}
如果(!number.equals())
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(其中,numberParams)
.withValue(Phone.NUMBER,号码)
。建立());
}
contentResolver.applyBatch(ContactsContract.AUTHORITY,OPS);
}
}
赶上(例外五)
{
e.printStackTrace();
成功= FALSE;
}
返回成功;
}
//要获得所有联系人的联系IDS使用下面的方法
/ **
*包含ID的所有接触和其中的@返回数组列表; BR />
*空的ArrayList,如果没有接触存在< BR />< BR />
*< B>注意:< / B>这方法要求的权限和LT; B> android.permission.READ_CONTACTS< / B>
* /
公众的ArrayList<字符串> getAllConactIds()
{
ArrayList的<字符串> contactList =新的ArrayList<字符串>();
光标光标= activity.managedQuery(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,DISPLAY_NAME ASC);
如果(光标!= NULL)
{
如果(cursor.moveToFirst())
{
做
{
INT _id = cursor.getInt(cursor.getColumnIndex(_ ID));
contactList.add(+ _标识);
}
而(cursor.moveToNext());
}
}
返回contactList;
}
私人布尔isEmailValid(字符串电子邮件)
{
字符串EMAILADDRESS = email.toString()修剪()。
如果(EMAILADDRESS == NULL)
返回false;
否则如果(emailAddress.equals())
返回false;
否则如果(emailAddress.length()&其中; = 6)
返回false;
其他 {
字符串EX pression = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\\.([a-z][a-z|0-9]*(\\.[a-z][a-z|0-9]*)?)$";
CharSequence的inputStr = EMAILADDRESS;
模式模式= Pattern.compile(如pression,
Pattern.CASE_INSENSITIVE);
匹配匹配= pattern.matcher(inputStr);
如果(matcher.matches())
返回true;
其他
返回false;
}
}
私人布尔匹配(字符串stringToCompare,字符串regularEx pression)
{
布尔成功= FALSE;
模式模式= Pattern.compile(regularEx pression);
匹配匹配= pattern.matcher(stringToCompare);
如果(matcher.matches())
成功= TRUE;
返回成功;
}
I have one existing contact, I need to add a work address to that existing contact. I am using the following code, but it's not working.
String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE +
"'" + " AND " + ContactsContract.CommonDataKinds.StructuredPostal.TYPE + "=?";
String[] phoneArgs = new String[]
{String.valueOf(ContactId), String.valueOf(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, STREET)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, CITY)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, REGION)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, POSTCODE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, COUNTRY)
.build());
this.context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Any solution for this?
解决方案
/**
* @param name name of the contact
* @param number mobile phone number of contact
* @param email work email address of contact
* @param ContactId id of the contact which you want to update
* @return true if contact is updated successfully<br/>
* false if contact is not updated <br/>
* false if phone number contains any characters(It should contain only digits)<br/>
* false if email Address is invalid <br/><br/>
*
* You can pass any one among the 3 parameters to update a contact.Passing all three parameters as <b>null</b> will not update the contact
* <br/><br/><b>Note: </b>This method requires permission <b>android.permission.WRITE_CONTACTS</b><br/>
*/
public boolean updateContact(String name, String number, String email,String ContactId)
{
boolean success = true;
String phnumexp = "^[0-9]*$";
try
{
name = name.trim();
email = email.trim();
number = number.trim();
if(name.equals("")&&number.equals("")&&email.equals(""))
{
success = false;
}
else if((!number.equals(""))&& (!match(number,phnumexp)) )
{
success = false;
}
else if( (!email.equals("")) && (!isEmailValid(email)) )
{
success = false;
}
else
{
ContentResolver contentResolver = activity.getContentResolver();
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] emailParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String[] nameParams = new String[]{ContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String[] numberParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();
if(!email.equals(""))
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,emailParams)
.withValue(Email.DATA, email)
.build());
}
if(!name.equals(""))
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,nameParams)
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
}
if(!number.equals(""))
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,numberParams)
.withValue(Phone.NUMBER, number)
.build());
}
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
}
catch (Exception e)
{
e.printStackTrace();
success = false;
}
return success;
}
// To get COntact Ids of all contact use the below method
/**
* @return arraylist containing id's of all contacts <br/>
* empty arraylist if no contacts exist <br/><br/>
* <b>Note: </b>This method requires permission <b>android.permission.READ_CONTACTS</b>
*/
public ArrayList<String> getAllConactIds()
{
ArrayList<String> contactList = new ArrayList<String>();
Cursor cursor = activity.managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, "display_name ASC");
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
contactList.add(""+_id);
}
while(cursor.moveToNext());
}
}
return contactList;
}
private boolean isEmailValid(String email)
{
String emailAddress = email.toString().trim();
if (emailAddress == null)
return false;
else if (emailAddress.equals(""))
return false;
else if (emailAddress.length() <= 6)
return false;
else {
String expression = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\\.([a-z][a-z|0-9]*(\\.[a-z][a-z|0-9]*)?)$";
CharSequence inputStr = emailAddress;
Pattern pattern = Pattern.compile(expression,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches())
return true;
else
return false;
}
}
private boolean match(String stringToCompare,String regularExpression)
{
boolean success = false;
Pattern pattern = Pattern.compile(regularExpression);
Matcher matcher = pattern.matcher(stringToCompare);
if(matcher.matches())
success =true;
return success;
}
这篇关于如何更新现有的联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文