Android编程更新联系人

时间:2014-08-05 23:18:00

标签: java android sdk contact

我正在尝试更新电话簿中特定记录的电话号码,这是我的代码,但由于某种原因,该字段未更新,我无法找到问题:

PS:我将权限放在清单文件中以写入联系人。

final ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(Phone.CONTENT_URI, null, null, null, null);

    //***********************
    change.setOnClickListener(new OnClickListener () 
    {
        public void onClick(View v) 
        {   
             while(cur.moveToNext())
             {
                  String id = cur.getString(cur.getColumnIndexOrThrow(Phone.DISPLAY_NAME ));
                     if(id.equals("Jack"))
                     {
                         cur.getColumnIndexOrThrow(Phone.DISPLAY_NAME );
                         String lookupKey = cur.getString(cur.getColumnIndex(Phone.LOOKUP_KEY));
                         Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);

                         ContentValues value = new ContentValues();
                         value.clear();
                         value.put(Phone.NUMBER, "885544");
                         cr.update(uri,value, null, null);

                     }

             }
        }
    }); 

1 个答案:

答案 0 :(得分:0)

这应该做的工作:

void updatePhoneNumber(String contactid, String phoneNo) {
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
        builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " +
              ContactsContract.Data.MIMETYPE + "=?"+ " AND " +
              ContactsContract.CommonDataKinds.Organization.TYPE + "=?",
              new String[]{String.valueOf(contactid), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
              String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
        builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNo);
        ops.add(builder.build());

        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (Exception e) {
            // handle error
        }
    }

}