在Android 2.2上为现有联系人添加新号码

在Android 2.2上为现有联系人添加新号码,android,contact,android-2.2-froyo,Android,Contact,Android 2.2 Froyo,我正在尝试向现有联系人添加新号码(或电子邮件或网站),但代码无法正常工作。代码如下: int rowId = cursor1.getInt(cursor1 .getColumnIndex(ContactsContract.RawContacts._ID)); ContentValues contentValues = new ContentValues(); contentValues.put(ContactsContract.Data.RAW_CONTACT_I

我正在尝试向现有联系人添加新号码(或电子邮件或网站),但代码无法正常工作。代码如下:

int rowId = cursor1.getInt(cursor1
                .getColumnIndex(ContactsContract.RawContacts._ID));
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rowId);
contentValues.put(ContactsContract.Data.MIMETYPE,
        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER,
        "45435345");
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
        Phone.TYPE_HOME);

ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValues(contentValues).build());

当代码运行时,没有错误,也没有变化。我很沮丧。任何帮助都会救我

也许您需要这两个权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />


您可能需要使用
ContentResolver
而不是ContentValues。

使用此代码在手机Contatcs中插入新的Contatct:

Intent intent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, Uri.parse("tel:" + currentNum.getText())); //currentNum is my TextView, you can replace it with the number directly such as Uri.parse("tel:1293827")
intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true); //skips the dialog box that asks the user to confirm creation of contacts
startActivity(intent);

享受你的代码:)-

当你运行代码时会发生什么?你需要执行ContentProviderOperation检查以下答案我已经解决了我的问题,但无论如何感谢你的回答。我认为问题是由我的错误代码造成的,它破坏了数据库中的数据结构。当我创建一个新的模拟器时,代码运行良好^_^