【发布时间】:2014-05-13 08:34:08
【问题描述】:
我正在创建一个简单的 SMS 应用程序,以开始为 Android 进行开发。我希望我的收件箱显示联系人姓名而不是他们的电话号码。
以下代码来自我的 onCreate() 方法:
// 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);
然后我在 onCreateLoader 方法中有以下内容:
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;
}
此代码使用带有 CursorLoader 的 LoaderManager 来获取收件箱中邮件的数量和正文。但是,我想在邮件正文中显示联系人姓名。
我查看了各种教程和建议,例如:How do i get the SMS Sender Contact (Person) saved name using "content://sms/inbox",但这些显示了如何在 UI 线程中使用 SimpleCursorAdapter 进行操作,这不是根据文档获取 SMS 收件箱的推荐方法(公共构造函数 - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html)
我希望在 onCreateLoader 方法中使用 PhoneLookup,但这似乎不起作用。
使用的SmsQuery接口如下:
/**
* 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;
}
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: android sms android-cursorloader