需要帮助...联系人匹配的短信
问题描述:
签出我的代码我一直在做一些短信应用程序的工作,现在我正在尝试将短信的地址与存储在联系人中的号码进行匹配...需要帮助...联系人匹配的短信
到目前为止,我无法匹配联系人并显示发件人的名称,因为模拟器上只包含两个联系人......它与第一个匹配,并在手机上显示其名称,但未找到单个接触...
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;
}
答
我没有完全理解你的编码here.But我收到了你requirement.So我的建议是,试图通过这种方式(我没试过过b的时间不够,现在ecause,但在逻辑上它应该工作):
public List<String> getSMS() {
List<String> sms = new ArrayList<String>();
//fetch all inbox sms
Cursor inboxCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null,null);
if(inboxCursor.getCount()>0)
{
inboxCursor.moveToFirst();
for(int i=0;i<inboxCursor.getCount();i++)
{
//get phone-number of sms
String address=inboxCursor.getString(inboxCursor.getColumnIndex("address"));
//fetch contacts with same phone-number
Cursor contactCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.NUMBER+"='"+address+"'",null,null);
if(contactCursor.getCount()>0)
{
contactCursor.moveToFirst();
// fetch the name associated with that number in contacts saved
String name=contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//add number,name,message to list
sms.add("Number: " + address+ " .Name: "+ name +".Message: " + body);
}
else
{
// for unknown contact
// add number,name,message to list
sms.add("Number: " + address+ " .Name: Unknown .Message: " + body);
}
inboxCursor.moveToNext();
}
}
return sms;
}
编辑:
将所有短信从每个联系人:
List<String> groupSms=new List<String>();
// retrieve all contacts
Cursor contacts=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,null);
if(contacts.getCount()>0)
{
contacts.moveToFirst();
for(int i=0;i<contacts.getCount();i++)
{
// get phone number
String phone_number=contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) ;
// retrieve sms of phone_number from inbox
Cursor sms=getContentResolver().query(Uri.parse("content://sms/inbox"), null, "address='"+phone_number+"'", null,null);
if(sms.getCount()>0)
{
sms.moveToFirst();
for(int j=0;j<sms.getCount();j++)
{
// get sms body
String text=sms.getString(sms.getColumnIndex("body"));
// add "phone_number" and "text" to a list
groupSms.add(phone_number+":"+text);
// move to next sms from phone_number
sms.moveToNext();
}
}
//move to next contact
contacts.moveToNext();
}
}
答
退房验证码...
String phone_number = contacts
.getString(contacts
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(null, "Stage 12");
// retrieve sms of phone_number from inbox
Cursor sms = getContentResolver().query(
Uri.parse("content://sms/inbox"), null,
"address='" + phone_number + "'", null, null);
Log.i(null, "Stage 13");
if (sms.getCount() > 0) {
Log.i(null, "Stage 14");
sms.moveToFirst();
for (int j = 0; j < sms.getCount(); j++) {
Log.i(null, "Stage 15");
// get sms body
String text = sms.getString(sms.getColumnIndex("body"));
// add "phone_number" and "text" to a list
groupSms.add(phone_number + ":" + text);
Log.i(null, "Stage 16");
// move to next sms from phone_number
sms.moveToNext();
}
}
// move to next contact
contacts.moveToNext();
}
}
Log.i(null, "Stage 9");
return groupSms;
下面提到的代码从不执行....它是上面的代码的一部分...
if (sms.getCount() > 0) {
Log.i(null, "Stage 14");
sms.moveToFirst();
for (int j = 0; j < sms.getCount(); j++) {
Log.i(null, "Stage 15");
// get sms body
String text = sms.getString(sms.getColumnIndex("body"));
// add "phone_number" and "text" to a list
groupSms.add(phone_number + ":" + text);
Log.i(null, "Stage 16");
// move to next sms from phone_number
sms.moveToNext();
}
它让我很头疼。我无法弄清楚为什么这个代码永远不会执行...我对我的星系运行的代码......
任何帮助家伙......?
@kashifmehmood:请看到我的编辑答案 – Hiral 2012-03-01 10:52:57
是非常感谢多数民众赞成我一直在寻找。 ...但现在我需要将同一联系人的所有短信分组......并且还有一些保存的联系人也包含国家代码......它只是国家代码的差异,但它不会显示..如何可以我完成了这两件事情? – kashifmehmood 2012-03-01 10:53:16
@ kashifmehmood:我已经更新了我的答案。您还应该尝试自己编写代码。我们无法为您编写完整的代码。从上面的代码中,您会明白它是如何工作的。尝试编写代码,然后将问题带入SO 。 – Hiral 2012-03-01 10:55:00