2014-04-27 55 views
0

我想弄清楚如何創建一個短信收件箱列表,就像從Android附帶的本機SMS信使應用程序的短信列表。我有下面的代碼,但是如果你有很多短信,它似乎非常慢。我希望得到一些更好的方法的指導。如何創建SMS列表?

我希望得到的地址,片段(或對話中的最後一條消息)和人(聯繫人顯示名稱),並將它們顯示在ListView中。

以下代碼用於檢索數據,但是當我在具有大量消息的電話上運行此操作時,Activity會在執行所有查詢時掛起幾秒鐘。

以下代碼片段是我用來檢索我提到的信息的鏈接方法。另外,我應該在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。這樣,你可以填充用戶界面隨着提取進行(理論上可以很好地工作......這一切都取決於你的需求真的:-))

+0

真棒,謝謝!我會看看這個。此外,這些查詢是收集生成SMS收件箱所需信息的首選方法嗎? – user3578624