使用预先填充的意图插入联系人

时间:2012-09-25 15:52:02

标签: android contact

我正在尝试使用意图插入联系人,我尝试使用Android Sdk中的这段代码,看到它here

ArrayList<ContentValues> data = new ArrayList<ContentValues>();

ContentValues row1 = new ContentValues();
row1.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
row1.put(Organization.COMPANY, "Android");
data.add(row1);

ContentValues row2 = new ContentValues();
row2.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
row2.put(Email.TYPE, Email.TYPE_CUSTOM);
row2.put(Email.LABEL, "Green Bot");
row2.put(Email.ADDRESS, "android@android.com");
data.add(row2);

Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
intent.putParcelableArrayListExtra(Insert.DATA, data);

startActivity(intent);

我得到一个空的InsertContact意图。它让我发疯,为什么它不起作用?

2 个答案:

答案 0 :(得分:2)

试试这个:

Intent addPersonIntent = new Intent(Intent.ACTION_INSERT);
                                        addPersonIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);

                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.NAME, "name");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "phone");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, "email");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "address");

                                        startActivityForResult(addPersonIntent, CREATE_NEW);

答案 1 :(得分:0)

也许您对DataOrganizationEmail类的实施是错误的。

这与完全实现Classes的代码相同:

ArrayList<ContentValues> data = new ArrayList<ContentValues>();

ContentValues row1 = new ContentValues();
     row1.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
     row1.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "Android");
data.add(row1);

ContentValues row2 = new ContentValues();
    row2.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
    row2.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM);
    row2.put(ContactsContract.CommonDataKinds.Email.LABEL, "Green Bot");
    row2.put(ContactsContract.CommonDataKinds.Email.ADDRESS, "android@android.com");
data.add(row2);

Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
    intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);    
startActivity(intent); 

注意:此代码仅适用于API Level 11及以上AS ContactsContract.Intents.Insert.DATA来自API级别11 Android Doc