Insert Contact to SIM from Android
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:开发者_JS百科
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?
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);
}
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
精彩评论