How to update E-mail, Address, Nickname, etc of a contact in Android?
Does anyone know how to do that? Even links to开发者_运维技巧 some tutorials would help.
I'm using a device with Android 2.1.
Please check below link
update contact details on Android
OR
Create XML file "editcontmain.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent" android:id="@+id/EditContacts"
android:onClick="@string/clickEvent" android:layout_height="wrap_content"
android:text="Edit Contacts" />
</LinearLayout>
And Create java file "EditContMain.java"
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class EditContMain extends Activity {
private static final int PICK_CONTACT = 0;
private static final int EDIT_CONTACT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editcontmain);
}
public void clickHandler(View v) {
switch (v.getId()) {
case R.id.EditContacts:
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
break;
default:
break;
}
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
Log.e("c", c.getCount() + "");
if (c.moveToFirst()) {
String contactId = c.getString(c
.getColumnIndex(ContactsContract.Contacts._ID));
Toast.makeText(this, "ID = " + contactId, Toast.LENGTH_LONG)
.show();
Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(Uri
.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI
+ "/" + contactId));
startActivityForResult(i, EDIT_CONTACT);
}
}
break;
}
}
}
i found it myself.. thanks everyone...
public void updateContact(String name, String phone,ContentResolver cr,Activity a)
{
String[] arr = {"DISPLAY_NAME","MIMETYPE","TYPE"};
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 = a.managedQuery(ContactsContract.Data.CONTENT_URI, null, null, null, null);
for(int i = 0; i<phoneCur.getColumnCount()-1;i++)
{
if(phoneCur.moveToNext())
{
String s = phoneCur.getString(phoneCur.getColumnIndex(Data.DISPLAY_NAME));
String s1 = phoneCur.getString(phoneCur.getColumnIndex(Data.MIMETYPE));
String s2 = phoneCur.getString(phoneCur.getColumnIndex(Phone.TYPE));
if(s!=null)
{
Log.e("@@@@@@@@@@@@#############",s);
}
if(s1!=null)
{
Log.e("111111111111#############",s1);
}
if(s2!=null)
{
Log.e("2222222222222#############",s2);
}
}
}
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "5657")
// .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name 21")
.build());
String where1 = ContactsContract.Data.DISPLAY_NAME + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ?";
String[] params1 = new String[] {name,"vnd.android.cursor.item/name"};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where1, params1)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name")
.build());
String[] params2 = new String[] {name,"vnd.android.cursor.item/email_v2"};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where1, params2)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, "Hi There")
.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();
}
}
I was update my contact in email,number,first name and last name,and if email and number not have then add using rowId
email and number also.
private void updateContact() {
if (getRawContactId(contactId).length() == 0) {
AppUtils.showToast(AddContactsActivity.this, getString(R.string.sorry_msg));
} else {
String name = etFname.getText().toString()+ " "+etLname.getText().toString().trim();
ContentResolver cr = 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>();
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where, nameParams)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, etFname.getText().toString().trim())
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, etLname.getText().toString().trim())
.build());
if (categoryContactData.getContact_mobile().length() > 0) {
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where, numberParams)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, etPhone.getText().toString().trim())
.build());
} else {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
//.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId))
.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId))
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, etPhone.getText().toString().trim())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
}
if (categoryContactData.getContact_email().length() > 0) {
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where, emailParams)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, etEmail.getText().toString().trim())
.build());
} else {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId))
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, etEmail.getText().toString().trim())
.build());
}
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
setResult(RESULT_OK);
finish();
// Toast.makeText(AddContactsActivity.this, "Updated the phone number of 'Sample Name' to: ", Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
精彩评论