How to get all contacts from phonebook & SIM card in Android?

独自空忆成欢 提交于 2019-12-02 21:28:49

Below is the code which 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();

Also For Sim contact only you can use below code:

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

EDITED:

To get the details of the contacts like home,mobile,fax etc. you need to check for that individually as 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;           
             }
            }
anupam sharma

The following code will return all the names of the contact and you can set in text view-

        Cursor cursor = null;
        String name, phoneNumber,image,email;
        try {
            cursor = getApplicationContext().getContentResolver()
                    .query(Phone.CONTENT_URI, null, null, null, null);
            int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
            int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
            int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_URI);
            cursor.moveToFirst();
            do {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                name = cursor.getString(nameIdx);
                phoneNumber = cursor.getString(phoneNumberIdx);
                image = cursor.getString(photoIdIdx);
                //email=cursor.getString(emailIdx);
                if(!phoneNumber.contains("*"))
                {
                    hashMap.put("name", "" + name);
                    hashMap.put("phoneNumber", "" + phoneNumber);
                    hashMap.put("image", "" + image);
                    //hashMap.put(email, ""+email);
                    hashMapsArrayList.add(hashMap);
                }
            } while (cursor.moveToNext());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();

    }

The following code will return all the names of the contact

  public static Cursor get(Context c){
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME };
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER
            + " = '1'";
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";

    Cursor cursor = c.getContentResolver().query(uri, projection,
            selection, null, sortOrder);
    return cursor;
}

To get all the data you can do the following

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.STARRED,
            ContactsContract.CommonDataKinds.Phone.TYPE };
    String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";

    Cursor cursor = context.getContentResolver().query(uri, projection,
            null, null, sortOrder);

From the cursor you can get all the data

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