Insert Contact to SIM from Android

女生的网名这么多〃 提交于 2019-12-07 03:40:17

问题


I have an issue while I try to copy a contact which exists in the android contacts application to the SIM card. Following 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 cName and cNumber variables. But when I print the log to see the value of the uri variable: it is null.

Can anyone please let me know if I have gone wrong somewhere in the code above for inserting to SIM?


回答1:


I just have implemented a simple code to insert contact in 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);
}



回答2:


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 completely understanding see this



来源:https://stackoverflow.com/questions/6274640/insert-contact-to-sim-from-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!