2012-08-16 2 views
0

현재 장치의 기본 텍스트 메시징 응용 프로그램에 저장된 문자 메시지 (SMS) 대화 상대의 연락처 정보 (이름 및 번호)를 가져오고 싶습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?메시징 앱에서 현재 연락처 가져 오기

+0

대화를 반복하고 이름을 가져온 다음 대화 상대를 찾으십니까? – Eric

+0

예 이것은 정확히 내가하고 싶은 것입니다 – Peter

답변

1

당신은 SMS에서 해결 된 모든 주소를 얻기 위해 아래의 URI를 조회 할 수 있습니다 및 MMS

content://mms-sms/conversations 

당신은 address 열을

ContentResolver contentResolver = getContentResolver(); 
    final String[] projection = new String[]{"*"}; 
    Uri uri = Uri.parse("content://mms-sms/conversations/"); 
    Cursor query = contentResolver.query(uri, projection, null, null, null); 
    String phone = ""; 
    while(query.moveToNext()){ 
    phone = query.getString(query.getColumnIndex("address")); 
    Log.d("test",phone); 
    } 

편집을 얻을 필요가 : 당신은을 확인할 수 있습니다 아래 함수는 here에서 복사했습니다. 이 기능 당신은 속도가 느린 것입니다 각각의 모든에 대한 번호를이를 호출하기 때문에 검색어와 일치하는이를 편집해야합니다

private String getContactNameFromNumber(String number) { 
    // define the columns I want the query to return 
    String[] projection = new String[] { 
      Contacts.Phones.DISPLAY_NAME, 
      Contacts.Phones.NUMBER }; 

    // encode the phone number and build the filter URI 
    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number)); 

    // query time 
    Cursor c = getContentResolver().query(contactUri, projection, null, 
      null, null); 

    // if the query returns 1 or more results 
    // return the first result 
    if (c.moveToFirst()) { 
     String name = c.getString(c 
       .getColumnIndex(Contacts.Phones.DISPLAY_NAME)); 
     c.close(); 
     return name; 
    } 
    c.close(); 
    // return the original number if no match was found 
    return number; 
} 

에 위 주소 열에서 선택 번호를 전달합니다.

+0

제가 어떻게 할 수 있는지 예를 알고 있습니까? – Peter

+0

수정 사항을 확인하십시오. – nandeesh

+0

고마워,하지만 전화에 저장된 SMS 또는 MMS 메시지가있는 연락처의 번호는 어디서 얻을 수 있습니까? – Peter