PDA

View Full Version : SIM contacts



caramare
07-14-2011, 05:37 AM
Hello,

I need to manege the phone contacs. This includes the contacts from the internal phone storage and the SIM card.

I have used the following code:

private Cursor getContacts(Uri uri) {
String[] projection = new String[] {PhoneLookup._ID,
PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER
};
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}

private void populateContactList() {
Cursor cursor = getContacts(uri);
String[] fields = new String[] {PhoneLookup.DISPLAY_NAME};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry,
cursor, fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);

}

But this code retrieves only the contacs from the internal phone storage and not from the SIM card.
I have made some research and I have found that the code below returns the contacs from the SIM card:

private ArrayList<String> retrieveSIMContacts() {
Uri uri15 = Uri.parse("content://sim/adn/");

ContentResolver resolver = getContentResolver();
Cursor results = resolver.query(uri15, null, null, null, null);

// Android 1.6 has a different URI
if(null == results) {
Uri uri16 = Uri.parse("content://icc/adn/");
results = resolver.query(uri16, null, null, null, null);
}

final ArrayList<String> simContacts = new ArrayList<String>();
final int nameIndex = results.getColumnIndex("name");
final int numberIndex = results.getColumnIndex("number");
final int idIndex = results.getColumnIndex("_id");

while (results.moveToNext()) {
final StringBuilder builder = new StringBuilder();
builder.append(results.getString(nameIndex)).appen d(" : ");
builder.append(results.getString(numberIndex)).app end(" : ");
builder.append(results.getInt(idIndex));

simContacts.add(builder.toString());
}
return simContacts;
}

Is this the only way to get the contacs from the SIM card or I can use the ContactsContract to get the SIM contacs?

Thank you very much,
Razvan