2

我正在尝试使用意图插入联系人,并且我尝试了从 Android Sdk 获取的这段代码,请在此处查看

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 意图。它让我发疯,为什么它不起作用?

4

2 回答 2

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);
于 2012-09-25T15:58:25.960 回答
0

也许您的Data,OrganizationEmailClass 的实现是错误的。

这是相同的代码,具有 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); 

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

于 2014-09-28T12:46:45.607 回答