联系意图插入城市,国家,ZipCode

时间:2012-02-08 05:35:57

标签: android android-intent

我正在使用以下代码插入联系人。

    intent = new Intent(Intent.ACTION_INSERT); 
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
    intent.putExtra(ContactsContract.Intents.Insert.COMPANY,ConstantData.mainTable.getCompany_name());
    intent.putExtra(ContactsContract.Intents.Insert.POSTAL,ConstantData.mainTable.getAddress1()+" "+ConstantData.mainTable.getAddress2()+" "+ConstantData.mainTable.getAddress3()+",City: America"); 
    intent.putExtra(ContactsContract.Intents.Insert.PHONE,ConstantData.mainTable.getTelephone_number());
    intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE,ConstantData.mainTable.getMobile_number()); 
    intent.putExtra(ContactsContract.Intents.Insert.EMAIL,ConstantData.mainTable.getEmail_address()); 

    startActivity(intent);

但是我无法插入City,Zip-code和Country,那么如何使用Intent插入它?

1 个答案:

答案 0 :(得分:0)

我添加了一个联系人,但没有使用Intent。这是代码:

void addingContact(String firstName, String phoneMobile, String street, String poBox, String city, String state, String zipCode, String country)
{
  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                            ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                                    .build());

                        //---------------------- Names
                            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                    .withValue(ContactsContract.Data.MIMETYPE,
                                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                                    .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
                                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName).build());

                        //--------------------- Mobile Number
                            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                    .withValue(ContactsContract.Data.MIMETYPE,
                                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneMobile)
                                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                                            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                                    .build());


//--------------------------------- address   
                               ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                        .withValue(ContactsContract.Data.MIMETYPE,
                                                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                                        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
                                        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
                                        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
                                        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
                                        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, 
                                                ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)
                                        .build());

try 
                            {
                                context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                            } 
                            catch (Exception e) 
                            {               
                                e.printStackTrace();
//                               Toast.makeText(this, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
}