Insert contact with SIM card with Android

I have a problem when I try to copy the contact that exists in the Android contacts application to the SIM card. Below is the code:

ContentValues cv = new ContentValues(); cv.put("tag", cName); cv.put("number", cNumber); Uri uri = context.getContentResolver().insert(SIM_CONTENT_URI, cv); Log.d(TAG_LOG, "URI is : " + uri); 

I have values ​​inside the cName and cNumber variables. But when I print a log to see the value of the uri variable: it is null.

Can someone please let me know if I am mistaken somewhere in the code above for insertion on SIM?

+6
source share
2 answers

I just implemented a simple code to insert a contact on a SIM card, maybe it can help you:

 private void insertSIMContact(String number, String name) { Uri simUri = Uri.parse("content://icc/adn"); ContentValues values = new ContentValues(); values.put("number", number); values.put("tag", name); getContentResolver().insert(simUri, values); } 
+1
source

Try this code ..

 String name = "First Family"; String phone = "0123456789"; ContentValues values = new ContentValues(); values.put(Data.DISPLAY_NAME, name); Uri rawContactUri = c.getContentResolver().insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); long contactId = Util.getContactId(c, rawContactId); System.out.println("rawContactId = " + rawContactId); System.out.println("contactId = " + contactId); values.clear(); values.put(Phone.NUMBER, phone); values.put(Phone.TYPE, Phone.TYPE_OTHER); values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Data.RAW_CONTACT_ID, rawContactId); c.getContentResolver().insert(Data.CONTENT_URI, values); values.clear(); values.put(Data.MIMETYPE, Data.CONTENT_TYPE); values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name); values.put(Data.RAW_CONTACT_ID, rawContactId); c.getContentResolver().insert(Data.CONTENT_URI, values); values.clear(); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name); values.put(Data.RAW_CONTACT_ID, rawContactId); c.getContentResolver().insert(Data.CONTENT_URI, values); public static long getContactId(Context context, long rawContactId) { Cursor cur = null; try { cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts.CONTACT_ID }, ContactsContract.RawContacts._ID + "=" + rawContactId, null, null); if (cur.moveToFirst()) { return cur.getLong(cur.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID)); } } catch (Exception e) { e.printStackTrace(); } finally { if (cur != null) { cur.close(); } } return -1l; } 

for full understanding see this

0
source

Source: https://habr.com/ru/post/890023/


All Articles