• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SIM contacts

caramare

Lurker
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)).append(" : ");
builder.append(results.getString(numberIndex)).append(" : ");
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
 
ContactsContract was introduced in Android 2.0 (Eclair), so you definitely wouldnt be able to use it for 1.5 or 1.6

Not sure if that's what you're asking though.
 
Yes I know that it ContactsContract was introduced in Android 2.0. My question is how I can get the contacts from the SIM card using ContactsContract ? Now when I use ContactsContract I get only the contacts from the Internal phone storage.

Thank you,
Razvan
 
ContactsContract is supposed to be an API that aggregates the contacts and abstracts the differences away for you. However, if you find that it isnt grabbing the ones you need, then why not try the way you are doing it for 1.5 and 1.6 and test it on some 2.0+ devices?

Basically, if you have a way of getting the raw contact info already, you almost dont need ContactsContract....
 
Thank you for your answer.
I didn't tested but I think using this Uri Uri uri16 = Uri.parse("content://icc/adn/"); I can get the raw contact.

As a last question. Does ContactsContract contains an Uri that get the Raw contact from the SIM card?

Have a nice day,
Razvan
 
Thank you for your answer.
I didn't tested but I think using this Uri Uri uri16 = Uri.parse("content://icc/adn/"); I can get the raw contact.

As a last question. Does ContactsContract contains an Uri that get the Raw contact from the SIM card?

Have a nice day,
Razvan

Yes, it does have a raw contact method for the db, I forget the name off the top of my head though.

edit: linky= http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
 
I didn't manage to get the SIM contacts using ContactsContract.RawContacts so I have used the code below and is working fine:

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;
}

Thank you,
Razvan
 
Back
Top Bottom