2010-07-28 35 views
8

我正在学习android。我正在尝试以编程方式更新联系人号码。任何人都可以帮助我,我该怎么做。如何使用Android更新联系人号码

我的努力是:

String lNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

ContentValues values = new ContentValues(); 

Uri lPhoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.NUMBER);      

values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "45323333")); 
getContentResover().update(lPhoneUri, values, ContactsContract.CommonDataKinds.Phone.NUMBER+"=?", new String[] { lNumber });   

回答

14

我认为你是非常有。以下内容使用新API更新联系人的WORK电话号码,假定该联系人已经有工作电话号码。

public void updateContact (String contactId, String newNumber, Activity act) 
    throws RemoteException, OperationApplicationException{ 

     //ASSERT: @contactId alreay has a work phone number 
     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
     String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + 
         Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?"; 
     String[] phoneArgs = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)}; 
     ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
       .withSelection(selectPhone, phoneArgs) 
       .withValue(Phone.NUMBER, newNumber) 
       .build()); 
     act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
    } 
+0

嗨,如果联系人没有工作电话,我们要更新工作电话号码? – MobiDev 2013-03-15 09:51:21

+0

我有更新WORK手机的问题,如果它存在,那么我可以更新它,但有问题时,WORK手机不在那里,我必须更新它。 – MobiDev 2013-03-15 09:58:38

+0

有没有一种方法可以使用特殊的URI通过id来更新它? – Roel 2015-08-04 07:32:32

3

只需稍作更改,如果您需要更新某些其他电话而不是工作电话,则可以使用此方法作为参数。

public void updateContact (String contactId, String newNumber, String phoneType) 
throws RemoteException, OperationApplicationException{ 

    //ASSERT: @contactId alreay has a work phone number 
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + 
        Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?"; 
    String[] phoneArgs = new String[]{contactId, phoneType}; 
    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
      .withSelection(selectPhone, phoneArgs) 
      .withValue(Phone.NUMBER, newNumber) 
      .build()); 
    this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
} 
1

此课程可能有助于更新联系人。

public class UpdateContact extends Activity { 

    private EditText txtName,txtPh; 
    private Button btnUpdate; 
    private TextView txt; 
    private static final String TAG_ID="id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_PHNO = "phNo"; 
    private static int id; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_update_contact); 
     txtName=(EditText)findViewById(R.id.txtNname); 
     txtPh=(EditText)findViewById(R.id.txtNPhNo); 
     btnUpdate=(Button)findViewById(R.id.btnUpdate); 
     txt=(TextView)findViewById(R.id.txtId); 

     Intent i=getIntent(); 
     id=i.getIntExtra(TAG_ID,0); 
     txtName.setText(i.getStringExtra(TAG_NAME)); 
     txtPh.setText(i.getStringExtra(TAG_PHNO)); 
     //to update contact on button click you can apply this 
     btnUpdate.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       DatabaseHandler handler=new DatabaseHandler(UpdateContact.this); 
       Contact contact=new Contact(); 
       contact.setID(id); 
       contact.setName(txtName.getText().toString()); 
       contact.setPhNo(txtPh.getText().toString()); 
       handler.UpdateContact(contact); 
       finish(); 
      } 
     }); 

    } 
} 
7

试试这个

    String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
          ContactsContract.Data.MIMETYPE + " = ? AND " + 
          String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? "; 

        String[] params = new String[] {Cname, 
         ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, 
         String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)}; 

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



        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
          .withSelection(where, params) 
          .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"9999999999") 
          // .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name 21") 

          .build()); 

        //email 
        String where3 = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
          ContactsContract.Data.MIMETYPE + " = ?"; 
        String[] params3 = new String[] {Cname,"vnd.android.cursor.item/email_v2"}; 
        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
          .withSelection(where3, params3) 
          .withValue(ContactsContract.CommonDataKinds.Email.DATA,"[email protected]") 
          .build()); 

通过使用这个我可以更新联系信息。

+0

不错!为我工作 – dhpratik 2015-01-13 10:58:18

相关问题