2

I'm trying to write a single contact to a SIM card.

I've scanned those questions so far: this, this, this, and this.

I've obtained the source of this nice library, but still, I don't see exported contacts from apps. And what's worse, I've filled up one of my sim cards without any possibility to clear it a bit, so I had to get another one.

Nothing helped.

Here is my code:

Uri simUri = Uri.parse("content://icc/adn");
OR RawContacts.CONTENT_URI;

SIM_ACCOUNT_NAME = "vnd.sec.contact.sim";
SIM_ACCOUNT_TYPE = "vnd.sec.contact.sim";



public static final void exportToSim(Context context,
            List<Contact> listContacts) {
        ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

        for (Contact contact : listContacts) {
            if (contact.getPhones() == null)
                continue;
            if (contact.getPhones().isEmpty())
                continue;
            /* Create empty contact */
            int backReference = operations.size();
            operations.add(ContentProviderOperation.newInsert(simUri)
                    .withValue(RawContacts.ACCOUNT_TYPE, SIM_ACCOUNT_TYPE)
                    .withValue(RawContacts.ACCOUNT_NAME, SIM_ACCOUNT_NAME)
                    .build());
            /* Add name Data */
            operations
                    .add(ContentProviderOperation
                            .newInsert(DATA_URI)
                            .withValueBackReference(Data.RAW_CONTACT_ID,
                                    backReference)
                            .withValue(Data.MIMETYPE,
                                    StructuredName.CONTENT_ITEM_TYPE)
                            .withValue(StructuredName.DISPLAY_NAME,
                                    contact.displayName).build());
            /* Add phone data */
            for (Phone phone : contact.getPhones()) {
                operations.add(ContentProviderOperation
                        .newInsert(DATA_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID,
                                backReference)
                        .withValue(Data.MIMETYPE,
                                CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                        .withValue(CommonDataKinds.Phone.NUMBER, phone.number)
                        .withValue(CommonDataKinds.Phone.TYPE,
                                CommonDataKinds.Phone.TYPE_MOBILE).build());
            }
        }

        try {
            context.getContentResolver().applyBatch(ContactsContract.AUTHORITY,
                    operations);
        } catch (RemoteException e) {
            Log.e(e.getClass().getSimpleName(), e.getMessage());
        } catch (OperationApplicationException e) {
            Log.e(e.getClass().getSimpleName(), e.getMessage());
        }
    }

In the case of simUri equal to Uri.parse("content://icc/adn") application crashes with UnsupportedOperationException,

In the case of simUri equal to RawContacts.CONTENT_URI contact is written in the RawCts table, and it's marked as contact from sim, but after the reload I don't see a contact anywhere. It seems, that it wasnt written straight to the SIM.

Values of the SIM_ACCOUNT_TYPE/NAME reflects the same account type/name pair from cts, exported by system application.

App's manifest contains both permissions: READ and WRITE _CONTACTS

Please, help. What code should I use to write a contact to SIM card correctly?

Thanks anyone who respond.

Community
  • 1
  • 1
UnknownJoe
  • 599
  • 5
  • 14
  • 30

1 Answers1

0

Create new sim contact using below code

void insertSIMContact() {

Uri simUri = Uri.parse("content://icc/adn");

ContentValues values = new ContentValues();
values.put("tag","Mitesh");
values.put("number", "1234");

getContentResolver().insert(simUri, values);
getContentResolver().notifyChange(simUri, null);

}
moDev
  • 5,248
  • 4
  • 33
  • 63