Android retrieve and display SMS conversation [closed]

吃可爱长大的小学妹 提交于 2021-01-29 08:14:36

问题


I'm trying to build a sms app. I've managed to get all the inbox message and display them in a thread but my approaching is probably wrong. I use a HashMap to store the address and based on it to delete existing message(only display the newest sms)

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();
}

and when I click a thread it will open an intent to display the conversation. The thing is, I have just managed to display only the inbox sms in a thread (no sent sms) like this

enter image description here

How to retrieve the whole conversation and display it like this

enter image description here

any help is greatly appreciated.


回答1:


Please check below code it might help you -

  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;
 }

and model class for 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;
      }

   }

and dont forget to add permission in your manifest -

 <uses-permission android:name="android.permission.READ_SMS" /> 


来源:https://stackoverflow.com/questions/28719070/android-retrieve-and-display-sms-conversation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!