Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

android - How can I programmatically add a contact?

I am trying to programmatically add a contact in Android. Here is my code:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,"Google")
    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,"[email protected]") .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,rawContactInsertIndex)
    .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
    .build());


ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
    .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType)
    .build());


ops.add(ContentProviderOperation.newInsert(ContactsContract
    .Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
    .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
    .withValue(ContactsContract.CommonDataKinds.Email.TYPE, emailType)
    .build());

try {
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    Context ctx = getApplicationContext();
    CharSequence txt = "Contact " + name +" added successfully";
    int duration = Toast.LENGTH_LONG;
    Toast toast = Toast.makeText(ctx, txt, duration);
    toast.show();
} catch (Exception e) {
    Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
}

Am I doing this correctly? How can I add an account name and account type?

NOTE: I don't have any account created. Is it mandatory to have account in order to add a contact?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Contact API storing contact as an invisible contact: How to make it visible? check this i hope it will be useful!

there is no need of creating any account in the phone!... i think their is some default account to do that!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...