public String getSmsAndSendBack()
{
String[] projection = new String[] { "address", "person", "body" };
StringBuilder str = new StringBuilder();
try {
ContentResolver cr = getContentResolver();
Cursor myCursor = cr.query(Uri.parse("content://sms/inbox"),
projection,
"address = ?", new String[] { "+86"+number},
"date desc");
str.append(processResults(myCursor, true));
} catch (SQLiteException ex) {
// Log.d(LOG_TAG, ex.getMessage());
}
return str.toString();
}
/**
*
* 处理短信结果
*
* @param cur
*
* @param all
* 用来判断是读一条还是全部读。后来没有用all,可以无视
*/
private StringBuilder processResults(Cursor cur, boolean all) {
StringBuilder str = new StringBuilder();
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String sms = null;
int nameColumn = cur.getColumnIndex("person");
int phoneColumn = cur.getColumnIndex("address");
int smsColumn = cur.getColumnIndex("body");
// do {
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneColumn);
if (phoneNumber.equals("+86"+number)) {
sms = cur.getString(smsColumn);
str.append(sms);
}
if (null == sms)
sms = "";
// } while (cur.moveToNext());
} else {
str.append("no result!");
}
return str;
}
![图片说明](https://img-ask.csdn.net/upload/201505/24/1432472589_284704.png)