我正在尝试构建一个短信应用。我已经设法获取所有收件箱消息并将其显示在一个帖子中,但我的接近可能是错误的。我使用HashMap存储地址并基于它来删除现有消息(仅显示最新的短信)
public void refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor = contentResolver.query(
Uri.parse("content://sms/inbox"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst())
return;
adapter.clear();
do {
String address = smsInboxCursor.getString(indexAddress);
if (!hashMap.containsKey(address))
hashMap.put(address, 0);
else
hashMap.put(address, hashMap.get(address) + 1);
try {
if (hashMap.containsKey(address)) {
String str = smsInboxCursor.getString(indexAddress) + ":\n"
+ smsInboxCursor.getString(indexBody) + "\n";
int count = adapter.getCount();
while(count>0){
String item = adapter.getItem(count);
String string = item.substring(0, item.indexOf(':'));
if (address.equals(string)) {
adapter.remove(item);
}
}
adapter.add(str);
}
} catch (Exception e) {
e.printStackTrace();
}
} while (smsInboxCursor.moveToNext());
smsInboxCursor.close();
}
当我点击一个帖子时,它会打开一个显示对话的意图。问题是,我刚刚设法在一个线程中显示收件箱短信(没有发送短信)这样
如何检索整个会话并像这样显示
非常感谢任何帮助。
答案 0 :(得分:1)
请查看以下可能对您有帮助的代码 -
public List<Sms> getAllSms() {
List<Sms> lstSms = new ArrayList<Sms>();
Sms objSms = new Sms();
Uri message = Uri.parse("content://sms/");
ContentResolver cr = mActivity.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
mActivity.startManagingCursor(c);
int totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new Sms();
objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
objSms.setAddress(c.getString(c
.getColumnIndexOrThrow("address")));
objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
objSms.setReadState(c.getString(c.getColumnIndex("read")));
objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
objSms.setFolderName("inbox");
} else {
objSms.setFolderName("sent");
}
lstSms.add(objSms);
c.moveToNext();
}
}
// else {
// throw new RuntimeException("You have no SMS");
// }
c.close();
return lstSms;
}
和sms的模型类 -
public class Sms{
private String _id;
private String _address;
private String _msg;
private String _readState; //"0" for have not read sms and "1" for have read sms
private String _time;
private String _folderName;
public String getId(){
return _id;
}
public String getAddress(){
return _address;
}
public String getMsg(){
return _msg;
}
public String getReadState(){
return _readState;
}
public String getTime(){
return _time;
}
public String getFolderName(){
return _folderName;
}
public void setId(String id){
_id = id;
}
public void setAddress(String address){
_address = address;
}
public void setMsg(String msg){
_msg = msg;
}
public void setReadState(String readState){
_readState = readState;
}
public void setTime(String time){
_time = time;
}
public void setFolderName(String folderName){
_folderName = folderName;
}
}
并且不要忘记在清单中添加权限 -
<uses-permission android:name="android.permission.READ_SMS" />