마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 전화 수신시 전화번호부 연락처명 가져오기

0 추천
안드로이드에서

이미 전화번호부에 등록 돼 있는  사람에게서 전화를 수신했을 경우

화면에 연락처명이 표시 돼잖아요?

그 연락처명 데이터를 이용하려 하는데

어떻게 하면 그 데이터를 얻을 수 있을까요?
용의덧니 (140 포인트) 님이 2017년 1월 4일 질문

1개의 답변

0 추천

service 와 receiver 를 통해 전화번호를 얻는 방법은 아래 링크를 참조하시면 될 거 같습니다.

http://stackoverflow.com/questions/15563921/how-to-detect-incoming-calls-in-an-android-device

전화번호를 얻어내셨으면 아래와 같은 메소드를 이용하여 연락처를 얻어내면 됩니다.

public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}

 

근영 (1,690 포인트) 님이 2017년 1월 4일 답변
...