如何在Android中以编程方式添加联系人?

时间:2012-02-01 12:56:22

标签: android android-contacts

我试过了,但没有添加联系人!

ContentResolver cr = this.getContentResolver();
ContentValues cv = new ContentValues();
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "sai1");
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "9640954335");
cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
cr.insert(ContactsContract.RawContacts.CONTENT_URI, cv);

2 个答案:

答案 0 :(得分:4)

尝试使用以下代码

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();

        Log.i("Line38", "Here");
           ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)  
                        .withValue(RawContacts.ACCOUNT_TYPE, AccountManager.KEY_ACCOUNT_TYPE)          
                        .withValue(RawContacts.ACCOUNT_NAME, AccountManager.KEY_ACCOUNT_NAME)          
                        .build());

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)      
                        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)      
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)      
                        .withValue(StructuredName.DISPLAY_NAME, "u232786seee")
                        .withValue(StructuredName.IN_VISIBLE_GROUP,true)
                        .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"23232343434")
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "4343")
        .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, "")
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "")
        .build());

        //Log.i("Line43", Data.CONTENT_URI.toString()+" - "+rawContactInsertIndex);

        try {
                getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }

并在清单文件中添加以下权限

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

答案 1 :(得分:2)

其他选项....

public void insert() {
    Intent intent = new Intent(
            ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
            ContactsContract.Contacts.CONTENT_URI);
    intent.setData(Uri.parse("tel:911"));//specify your number here
    intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Emergency USA");
    startActivity(intent);
    Toast.makeText(this, "Record inserted", Toast.LENGTH_SHORT).show();
}