Sam Kim
Sam Kim

Reputation: 31

Search for a contact's phone number by name in Android

I have successfully implemented a search in Android to find a contact's name by voice recognition. Now I would like to find the number by voice recognition.

I tried already this How to call Android contacts list?, but it doesn't work with my SGS2. I think the source is old version. That's why it doesn't work.

How can I find the number by name which is result from voice recognition in Android Contacts?

Upvotes: 1

Views: 2203

Answers (1)

Sam Kim
Sam Kim

Reputation: 31

    private void getContactData() {
        Cursor phoneCursor = null;
        contactList = new HashMap<String,String>();

      try{
             // 주소록이 저장된 URI
           Uri uContactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

          // 주소록의 이름과 전화번호의 열 이름
          String strProjection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;

          // 주소록을 얻기 위한 쿼리문을 날리고 커서를 리턴
          phoneCursor = getContentResolver().query(uContactsUri, null, null, null, strProjection);
          phoneCursor.moveToFirst();

          String name = "";
          String phoneNumber = "";

           // 주소록의 이름
           int nameColumn = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME);
           // 주소록의 전화번호
           int phoneColumn = phoneCursor.getColumnIndex(Phone.NUMBER);

           while(!phoneCursor.isAfterLast()){
               name = phoneCursor.getString(nameColumn);
               phoneNumber = phoneCursor.getString(phoneColumn);

               // HashMap에 data 넣음 
               contactList.put(name, phoneNumber);
               phoneCursor.moveToNext();
            }
        }
        catch(Exception e){
            Log.e("[SmsMain] getContactData", e.toString());
        }
        finally{
           if(phoneCursor != null){
              phoneCursor.close();
              phoneCursor = null;
           }
        }
    }

This is the source to get a contact DB.

I solved by searching.

Upvotes: 2

Related Questions