Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimized way to list all contact numbers and sms?
    primarykey
    data
    text
    <p>I'm developing a small application so I can get more experience of Android development. I wrote a method that list all of contact number and total messages (just like GoSMS or default SMS Application). But problem that I'm facing right now is slow performance. Below is what I have done to get the result.</p> <p>Sample result: Mr AAA (10000)</p> <p>Steps:</p> <ol> <li>Get all SMS thread ID</li> <li>Loop and get total messages belong to each thread ID.</li> <li>Get the contact number belong to that thread.</li> <li>Use that number and PhoneLookup to get contact name.</li> </ol> <p>Here is the method:</p> <pre><code>public void populateContactList() { // Get all sms threads Cursor smsAddressCursor = getContentResolver().query( SMSCVar.CONTENT_URI, new String[] { "DISTINCT "+SMSCVar.THREAD_ID}, null, null, null); while(smsAddressCursor.moveToNext()) { Contact c = new Contact(); // Get thread_id String thread_id = smsAddressCursor.getString(smsAddressCursor.getColumnIndex(SMSCVar.THREAD_ID)); // Get total messages Cursor totalMessage = getContentResolver().query( SMSCVar.CONTENT_URI, new String[] {"count("+SMSCVar.BODY+")"}, SMSCVar.THREAD_ID+" = ?", new String[] {thread_id}, null); totalMessage.moveToNext(); c.setNumberOfSMS(totalMessage.getInt(0)); totalMessage.close(); // Get number Cursor number = getContentResolver().query( SMSCVar.CONTENT_URI, new String[] {SMSCVar.ADDRESS}, SMSCVar.THREAD_ID+" = ?", new String[] {thread_id}, null); number.moveToNext(); String pNumber = number.getString(0); number.close(); // Get contact name Uri uriPhonenumber = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI , Uri.encode(pNumber)); Cursor contactDisplayName = getContentResolver().query( uriPhonenumber, new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); // If cursor is not null and has at least one result if(!contactDisplayName.isNull(0) &amp;&amp; contactDisplayName.getCount() &gt; 0) { // Get contact name for display contactDisplayName.moveToNext(); c.setContactName(contactDisplayName .getString(contactDisplayName .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME))); } else { // Get contact number for display c.setContactName(pNumber); } // Don't get confuse here, setContactNumber method is not used for display purpose. c.setContactNumber(pNumber); contactListAdapter.add(c); contactDisplayName.close(); } smsAddressCursor.close(); } </code></pre> <p>I've coded to close cursor properly but I still get GARBAGE COLLECTOR messages and slow retrieving time (5 seconds). My SMS is over 11000 messages.</p> <p>So please assist me! </p> <p>Thank you!</p> <p>P/S: I'm not an English native speaker so I've tried my best to make my question easy to understand. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload