【发布时间】:2012-03-01 05:35:10
【问题描述】:
检查我的代码我一直在做一些关于短信应用程序的工作,现在我正在尝试将短信随附的地址与联系人中存储的号码进行匹配...
到目前为止,我无法匹配联系人并显示发件人的姓名,因为在模拟器上仅包含两个联系人...它匹配第一个并显示其姓名,而在电话上却找不到单个联系人...
public List<String> getSMS() {
List<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,
null);
ContentResolver cr = getContentResolver();
Cursor contactsCur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
Cursor phoneCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
"DISPLAY_NAME = '" + name + "'", null, null);
Log.v(LOG_TAG, "going in loop");
if (contactsCur.getCount() > 0) {
Log.v(LOG_TAG, "inside loop");
while (contactsCur.moveToNext()) {
Log.v(LOG_TAG, "cursor moving to next");
id = contactsCur.getString(contactsCur
.getColumnIndex(ContactsContract.Contacts._ID));
Log.i(LOG_TAG,"this is id: " + id);
name = contactsCur
.getString(contactsCur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null);
Log.i(LOG_TAG, "This is name "+name);
while (phones.moveToNext())
{
number = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
String aNumber=number.replaceAll("-", "");
Log.i(LOG_TAG,"this is number :"+aNumber);
Log.v(LOG_TAG, "getting address and message");
while (cur.moveToNext()) {
String address = cur.getString(cur
.getColumnIndex("address"));
if(address.equals(aNumber))
address=name;
String body = cur.getString(cur
.getColumnIndexOrThrow("body"));
sms.add("Number: " + address+ " .Message: " + body);
}
Log.v(LOG_TAG, "closing phone cursor");
phoneCur.close();
}
}
}
return sms;
}
【问题讨论】:
标签: android