I am creating a simple SMS app just to start with developing for Android. I want my inbox show the contacts name rather than their number.
The following code is from my onCreate() method:
// Create adapter and set it to the list view.
final String[] fromFields = new String[] {
SmsQuery.PROJECTION[SmsQuery.ADDRESS], SmsQuery.PROJECTION[SmsQuery.BODY] };
final int[] toViews = new int[] { R.id.text_view_name, R.id.text_view_message };
mAdapter = new SimpleCursorAdapter(this, R.layout.convo_list,
null, fromFields, toViews, 0);
listView.setAdapter(mAdapter);
// Simple query to show the most recent SMS messages in the inbox.
getSupportLoaderManager().initLoader(SmsQuery.TOKEN, null, this);
Then I have the following in the onCreateLoader method:
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
if (i == SmsQuery.TOKEN) {
// Fetch all SMS messages in the inbox, date desc.
return new CursorLoader(this, SmsQuery.CONTENT_URI, SmsQuery.PROJECTION,
null, null, SmsQuery.SORT_ORDER);
}
return null;
}
This code uses a LoaderManager with a CursorLoader to get the number and body of the messages in the inbox. I however would like to display the contacts name with the body of the message.
I've looked at various tutorials and suggestions like: How do i get the SMS Sender Contact (Person) saved name using "content://sms/inbox" but these show how to do it with a SimpleCursorAdapter in the UI thread which is not the recommended way to get the SMS inbox according to the documentation (Public Constructors - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html)
I was hoping to use a PhoneLookup in the onCreateLoader method but that doesn't seem to work.
The SmsQuery interface that is used is as follows:
/**
* A basic SmsQuery on android.provider.Telephony.Sms.Inbox
*/
private interface SmsQuery {
int TOKEN = 1;
static final Uri CONTENT_URI = Telephony.Sms.Inbox.CONTENT_URI;
static final String[] PROJECTION = {
Inbox._ID,
Inbox.THREAD_ID,
Inbox.ADDRESS,
Inbox.BODY,
Inbox.PERSON,
};
static final String SORT_ORDER = Telephony.Sms.Inbox.DEFAULT_SORT_ORDER;
int ID = 0;
int THREAD_ID = 1;
int ADDRESS = 2;
int BODY = 3;
int PERSON = 4;
}
Any help would be greatly appreciated. Thank you.