使用下面代碼在用戶和數字之間獲取整個會話:
Uri SMS_INBOX = Uri.parse("content://sms/");
String selection = "thread_id = " + thread_id;
final String[] projection = new String[] { "*" };
Cursor c = getContentResolver().query(SMS_INBOX, projection, selection,null, "date");
startManagingCursor(c);
String[] body = new String[c.getCount()];
String[] address = new String[c.getCount()];
if (c.moveToFirst()) {
for (int j = 0; j < c.getColumnCount(); j++)
Log.w("ColumnName", c.getColumnName(j));
for (int i = 0; i < c.getCount(); i++) {
body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();
address[i] = c.getString(c.getColumnIndexOrThrow("address")).toString();
Log.d("address-" + i, address[i]);
Log.d("body-" + i, body[i]);
String subject = c.getString(c.getColumnIndexOrThrow("_id")).toString();
Log.d("_id-" + i, subject);
String thread = c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
Log.d("thread_id-" + i, subject);
Log.d("----", "----");
c.moveToNext();
}
}
使用這段代碼在一個會話中獲得所有的信息,但是我不能找出哪個數字發送哪條信息。
如果獲取 "address" 列,就一直返回相同的數字,所以不知道這條信息是用戶發的,還是其它的號碼。
列 總是只給出另一個數字。如果你想區分發送信息和接收信息,你應該使用'type'列。
body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();
if(c.getString(c.getColumnIndex("type")).equalsIgnoreCase("1")){
// sms received
msg_state[i]=1;
}
else {
//sms sent
msg_state[i]=0;
}