How to properly set an android intent with the SHO

2019-08-10 20:32发布

问题:

I would like to use the internal Contact activity to create a new Contact. Here is the code:

        Intent i = new Intent();
    i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    i.setData(Uri.fromParts("tel", "12345678", null));
    i.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true);
    i.putExtra(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK); 
    i.putExtra(ContactsContract.Intents.Insert.EMAIL, "naoknaoknaok@gmail.com");
    i.putExtra(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
    i.putExtra(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "Ide");
    i.putExtra(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Vele");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.COMPANY, "Company name");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK);
    i.putExtra(ContactsContract.CommonDataKinds.Organization.LABEL, "label");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.TITLE, "job title");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, "department");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, "job description");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.SYMBOL, "symbol");
    i.putExtra(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, "office location");

But this code only partially works. Only the phone number and the email address are visible in the activity, the other fields are unset.

Here is the screenshot from the emulator: edit_contact1

Sorry for not including it, but i don't have enough reputation to include images.

Any help would be greatly appreciated !

Some update: the following code sets the 'Given name' field:

i.putExtra(ContactsContract.Intents.Insert.NAME, "simple name");

回答1:

i'd imagine that only the constant keys in ContactsContract.Intents.Insert.* are understood by the intent receiver. that would make sense.