如何在Android中更新联系人的电子邮件,地址,昵称等?

时间:2011-06-24 09:27:02

标签: android contacts

有谁知道怎么做?甚至链接到一些教程也会有所帮助。

我正在使用Android 2.1设备。

3 个答案:

答案 0 :(得分:13)

请查看以下链接

update contact details on Android



创建XML文件“ 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>

创建java文件“ 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;    
        }
    }
}

答案 1 :(得分:9)

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



}

答案 2 :(得分:2)

我在电子邮件,号码,名字和姓氏中更新了我的联系人,如果没有电子邮件和号码,那么也使用rowId电子邮件和号码添加。

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