2011년 12월 1일 목요일

Android 전화번호부 조회

1) 조회 결과를 저장할 Value Object Class 선언

public class Addressbook {
    private String name;
    private String phnnumber;
  
 public void setPhnnumber(String phnnumber) {
  this.phnnumber = phnnumber;
 }
 public String getPhnnumber() {
  return phnnumber;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getName() {
  return name;
 }

}

2) Contacts 조회 Method Sample
    protected Addressbook[] getFriendsfromAddressBook()
    {
     String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
    
     Cursor c = getContentResolver().query( Phone.CONTENT_URI, projection, null, null, null);

     Addressbook[] addressfriend = new Addressbook[ c.getCount() ];
    
     c.moveToFirst();
    
     int idx = 0;
     String bookname = null;
     String booknum = null;
    
     try {
          if ( ! c.isAfterLast() )
         {
                do
                {
                
                 bookname = c.getString( 0 );
                 booknum = c.getString( 1 );

                 addressfriend[idx] = new Addressbook();
                 addressfriend[idx].setName( bookname );
                 addressfriend[idx].setPhnnumber( booknum );
                
                 idx++;
               
                } while ( c.moveToNext() );
            }
        } catch (Exception e ) {
         showToastMsg ( "주소록 정보를 가져올 수 없습니다.\n" + e.toString());
         return null;
        }
        finally {
         c.close();
        }
      
        return addressfriend;

    }

댓글 없음:

댓글 쓰기