签出我的代码我一直在做短信应用程序的一些工作,我现在正在尝试将短信附带的地址与已存储在联系人中的号码相匹配......
到目前为止,我无法匹配联系人并显示发件人的姓名,因为在模拟器中只包含两个联系人...它与第一个联系人匹配并在手机上显示其名称时找不到单个联系人...
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;
}
答案 0 :(得分:1)
查看此代码....
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();
}
它让我非常头疼...我无法弄清楚为什么这段代码永远不会执行...我在银河系上运行代码......
任何帮助人......?
答案 1 :(得分:0)
我没有完全理解你在这里编写的内容。但是我得到了你的要求。所以我的建议是 - 尝试这种方式(我没有尝试过,因为现在时间不够,但从逻辑上说它应该有效) :
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();
}
}
答案 2 :(得分:0)
当您处理电话号码时,它(https://code.google.com/p/libphonenumber/)可能有助于推广它......:)