Below is the code that shows an easy way to read all phone numbers and names :
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close();
In addition, only the code below can be used for Sim:
private void allSIMContact() { try { String m_simPhonename = null; String m_simphoneNo = null; Uri simUri = Uri.parse("content://icc/adn"); Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null); Log.i("PhoneContact", "total: "+cursorSim.getCount()); while (cursorSim.moveToNext()) { m_simPhonename =cursorSim.getString(cursorSim.getColumnIndex("name")); m_simphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number")); m_simphoneNo.replaceAll("\\D",""); m_simphoneNo.replaceAll("&", ""); m_simPhonename=m_simPhonename.replace("|",""); Log.i("PhoneContact", "name: "+m_simPhonename+" phone: "+m_simphoneNo); } } catch(Exception e) { e.printStackTrace(); } }
Edition:
To get information about contacts, such as home, mobile, fax, etc., you need to check this separately, as shown below:
while (phone_crsr.moveToNext()) { int phone_type = phone_crsr.getInt(phone_crsr.getColumnIndex(Phone.TYPE)); switch (phone_type) { case Phone.TYPE_HOME: phone_home =phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(this, "home"+phone_home, Toast.LENGTH_LONG).show(); break; case Phone.TYPE_MOBILE: phone_mob=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(this, "mob"+phone_mob, Toast.LENGTH_LONG).show(); break; case Phone.TYPE_WORK: phone_work=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(this, "work"+phone_work, Toast.LENGTH_LONG).show(); break; } }
GrIsHu
source share