如何以编程方式创建联系人

时间:2011-12-07 10:57:00

标签: android contacts contact android-contacts contactscontract

  

可能重复:
  How to add new contacts in android

public boolean createContact(String name, String number, String email) 
{
        boolean success = true;

        try
        {
            ContentValues contentValues = new ContentValues();

            ContentResolver contentResolver  = getContentResolver();

            contentValues.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
            contentValues.put(Phone.NUMBER, "123254");
            Uri uri = contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, contentValues);

            if(uri==null)
            {
                success = false;
            }

        }
        catch (Exception e) 
        {
            e.printStackTrace();
            success = false;
        }
        return success;
}

我收到NullPointer异常我不知道为什么我还指定了 WRITE_CONTACTS 权限。请帮帮我............

1 个答案:

答案 0 :(得分:6)

ContentValues values = new ContentValues();
            values.put(Data.RAW_CONTACT_ID, 001);
            values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
            values.put(Phone.NUMBER, "            1-800-GOOG-411      ");
            values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
            values.put(Phone.LABEL, "free directory assistance");
            Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

使用上面的代码。你没有提供身份证。 http://developer.android.com/reference/android/provider/ContactsContract.Data.html