2014-04-27 6 views
0

Android와 함께 제공되는 기본 SMS 메신저 앱에서 SMS 목록과 같은 SMS받은 편지함 목록을 만드는 방법을 알아 내려고합니다. 다음 코드를 가지고 있지만 SMS 메시지가 많으면 속도가 매우 느립니다. 나는 더 나은 방법에 대한 지침을 원했다.SMS 목록을 만드는 방법은 무엇입니까?

나는 주소, 스 니펫 (또는 대화의 마지막 메시지) 및 사람 (연락처 표시 이름)을 가져 와서 ListView에 표시하려고합니다.

다음 코드는 데이터를 검색하는 데 사용되지만 많은 메시지가있는 전화에서이 코드를 실행하면 모든 쿼리가 수행되는 동안 활동이 몇 초 동안 중단됩니다.

다음 코드 단편은 언급 한 정보를 검색하는 데 사용하는 연결 방법입니다. 또한 Activity 나 그 밖의 스레드에서 스레드를 사용해야합니까? 쿼리를 수행하고 ListView를 업데이트하는 스레드?

미리 도움 주셔서 감사합니다.

private void getConversations(ArrayList<Conversation> conversationList){ 
    Uri uri = Uri.parse("content://sms/conversations"); 
    String[] selection = {"thread_id", "snippet"}; 
    Cursor cur = context.getContentResolver().query(uri, selection, null, null, "date DESC"); 

    if(cur.getCount() != 0){ 

     while(cur.moveToNext()) { 
      String thread_id = cur.getString(cur.getColumnIndex("thread_id")); 
      String snippet = cur.getString(cur.getColumnIndex("snippet")); 

      Conversation conversation = new Conversation(thread_id, snippet); 
      conversationList.add(conversation); 
     } 
    } 
    cur.close(); 
} 

private void getAddresses(ArrayList<Conversation> conversationList){ 
    Uri uri = Uri.parse("content://sms"); 
    String[] selection = {"address"}; 


    for(Conversation conversation : conversationList){ 

     Cursor cur = context.getContentResolver().query(uri, selection , "thread_id=?", new String[] {conversation.getThread_id()}, null); 
     if(cur.getCount() != 0){ 
      cur.moveToFirst(); 
      conversation.setAddress(cur.getString(cur.getColumnIndex("address"))); 
     } 
     cur.close(); 
    } 

} 

private void getDisplayName(ArrayList<Conversation> conversationList){ 
    Log.d(TAG, "Adding display names"); 
    for(Conversation conversation: conversationList) { 
     Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, conversation.getAddress()); 
     Cursor cur = context.getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); 

     if(cur.moveToFirst()){ 
      conversation.setPerson(cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME))); 
     } 
     cur.close(); 
    } 
} 

답변

0

예, 올바른 경로에 있습니다. AsyncTask을 사용하여 doInBackground 메서드에서 지금 수행중인 작업을 수행 할 수 있으며 작업이 끝나면 onPostExecute 메서드를 사용하여 결과를 UI에 게시하면됩니다.

대화를 통해 메시지를 그룹화 할 때 onProgressUpdate를 사용하여 UI를 업데이트 할 수도 있습니다. 이렇게하면 가져 오기가 진행되는 동안 UI가 채워질 수 있습니다. 이론적으로는 잘 작동 할 수 있습니다. 모두 사용자의 필요에 따라 실제로 제공됩니다 .-))

+0

감사합니다. 나는 이것에 대해 살펴볼 것이다. 또한 이러한 질문은 SMS받은 편지함을 생성하는 데 필요한 정보를 수집하는 데 선호되는 방법입니까? – user3578624

관련 문제