Android

안드로이드 전화번호부에 저장된 이름 가져오기

박진만 2014. 3. 13. 18:17
반응형

안드로이드 연락처에 저장된 전화번호를 가지고 이름을 확인하고 싶은 경우 아래 코드를 사용하면 된다.

AndroidManifest.xml 에 퍼미션 추가

<uses-permission android:name="android.permission.READ_CONTACTS" />

java 파일

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(전화번호)); 
String[] projection = new String[]{ContactsContract.PhoneLookup.DIAPLAY_NAME};

String diaplayName = "";

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

if(cursor != null){
  
  if(cursor.moveToFirst()){

    diaplayName = cursor.getString(0);
  
  }

  cursor.close();

}

위 소스코드 전화번호에 조회할 전화번호 넣어주면 끝!

반응형