Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, create a contact at SIM programmatically

I am trying to create a contact at SIM(Nexus 5 Marshmallow).

    String name = (String)nameEditText.getText().toString();
    String phoneNumber = (String)phoneNumberEditText.getText().toString();

    String result = null;

    ArrayList<ContentProviderOperation> ops = new ArrayList < ContentProviderOperation > ();

    ops.add(ContentProviderOperation.newInsert(
            ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim")
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "SIM")
            .build());

    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.DISPLAY_NAME,
                    name).build());

    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, phoneNumber)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
            .build());

    // Asking the Contact provider to create a new contact
    try {
        getContentResolver().applyBatch("com.android.contacts", ops);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.toString());
    }

But it creates a contact at the phone memory.

As the first applyBatch's argument i also tried "com.android.contacts", "com.android.contacts.sim", "content://icc/adn". It doesn't work properly too.

like image 890
Yahor Zhylinski Avatar asked Sep 19 '26 04:09

Yahor Zhylinski


1 Answers

I came across the same question and been looking for answer.
What I do is change the ContactsContract.RawContacts.ACCOUNT_TYPE value to com.android.sim like this:

withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.android.sim")

and it works fine.

like image 54
Z.Xin Avatar answered Sep 20 '26 19:09

Z.Xin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!