Adding contacts to sim card

I am trying to add contacts to SIM Card, they seem to be added to the SIM Card but they're only visible after rebooting the device (even to the native People app)..

Here is the code I'm using:

final Uri uri = Uri.parse("content://icc/adn");
ContentValues mContentValue = new ContentValues();
mContentValue.put("tag", contactName);
mContentValue.put("number", contactNumber);
getContentResolver().insert(uri, mContentValue);

is it a problem with sync'ing the contacts db after creating the contacts? if so, is there a way, may be, to force android to sync the contacts' database with the SIM Card?


EDIT

Ok, let me explain my problem again. Here is man, who had same problem, but there is no any answer (when adding contacts to sim card, they are only displayed after rebooting the device), so I need to ask it again.
ACCOUNT_TYPE: com.android.contacts.sim and ACCOUNT_NAME: SIM are wrong for my phone. I got right from ContactsContract.Settings.CONTENT_URI: com.android.huawei.phone - Phone and com.android.huawei.sim - sim1. Here is my code:

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

Uri simUri = Uri.parse("content://icc/adn");
ContentValues values = new ContentValues();
values.put("tag", "NAME");
values.put("number", "77777777777");
try {
    Uri uri =  context.getContentResolver().insert(simUri, values);

    int efid = 0,index = 0;
    Pattern pattern = Pattern.compile("^content://icc/adn/(\\d+)/(\\d+)$*");
    Matcher matcher = pattern.matcher(uri.toString());
    if(matcher.matches()) {
        index = Integer.parseInt(matcher.group(1));
        efid = Integer.parseInt(matcher.group(2));
    }

    ArrayAdapterSpinner.Item item = adapter.getItem(spinner.getSelectedItemPosition());

    op.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.android.huawei.sim")
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "sim1")
            .withValue(ContactsContract.RawContacts.SYNC1,efid)
            .withValue(ContactsContract.RawContacts.SYNC2,index)
            .build());
    op.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());
    op.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, "77777777777")
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
            .build());

    try {
        ContentProviderResult[] cprs = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op);
    } catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }
} catch(Exception e) {
    Log.e("ERROR2", e.toString());
}

But result is:
1. Contact in icc/adn is created
2. Contact in RawContacts is created, but ACCOUNT_TYPE and ACCOUNT_NAME are replaced with com.android.huawei.phone - Phone

javaandroidcontactssim-card

Answers

answered 1 year ago Sahil #1

Maybe it can help

 try{
                // add a row to the RawContacts table
         ContentValues values = new ContentValues();
         values.put(RawContacts.ACCOUNT_TYPE, "com.android.contacts.sim");
         values.put(RawContacts.ACCOUNT_NAME, "SIM");
         Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);

        // get the ID of the newly-added line
 long rawContactId = ContentUris.parseId(rawContactUri);

        // add a "name" line to the Data table, linking it to the new RawContact
        // with the CONTACT_ID column
 values.clear();
 values.put(Data.RAW_CONTACT_ID, rawContactId);
 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
 values.put(StructuredName.DISPLAY_NAME, "Name");
 cr.insert(Data.CONTENT_URI, values);
  getContentResolver().notifyChange(Uri_Here,null); 
        // add a "phone" line to the Data table, linking it to the new RawContact
        // with the CONTACT_ID column
 values.clear();
 values.put(Data.RAW_CONTACT_ID, rawContactId);
 values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
 values.put(Phone.NUMBER, "+12345678901");
 values.put(Phone.TYPE, Phone.TYPE_MOBILE);
 cr.insert(Data.CONTENT_URI, values);
getContentResolver().notifyChange(Uri_Here,null); 
//New Edit
}
catch(Exception e){
    //exception handling
}

comments powered by Disqus