Thursday, 20 March 2014

Get the stored contacts details in Android

Get the list of all  stored contact details of phone through code;

In below code getting all the contact and put into List having to value Contact Name and Contact Number;


      
       private  void GetAllcontactsList()
       {

              if(objContactCol.isEmpty())
              {
              //boolean IsNumberFound = false;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
               null, null, null, null);

        String contactDetails = "";
        if (cur.getCount() > 0) {
           while (cur.moveToNext()) {
               String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
               String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//                   System.out.println("name : " + name + ", ID : " + id);
                 String contactNumbers =  "";
                 //String Values =  "name : " + name ;
              //   contactNumbers = contactNumbers + " " + Values;
                             
                  
                   // get the phone number
                   Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                          new String[]{id}, null);
                  
                 //  PhnNumber = PhnNumber.replace("+91", "");
                   ContactCollection objCol = new ContactCollection();
                   while (pCur.moveToNext()) {
                        
                         String phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      // if(phone.contains(PhnNumber))
                       //{
                    //   contactNumbers =  "name : " + name + " \n phone :" + phone;
                     //  IsNumberFound = true;
                      // break;
                       //}
                        // contactNumbers =  "name : " + name + " \n phone :" + phone;
                        // System.out.println("phone" + phone);
                        
                         objCol.setContactName(name);
                         objCol.setNum(phone);
                        
                   }
                   objContactCol.add(objCol);
                 /*  if(IsNumberFound == true)
                   {
                   contactDetails = contactDetails + " \n \n " + contactNumbers;
                   break;
                   }*/
                   contactDetails = contactDetails + " \n \n " + contactNumbers;
                   pCur.close();
               }
           }
        }
              }
                   
       }

           

No comments:

Post a Comment