获取文本短信详细信息时出错?

时间:2014-02-25 07:52:38

标签: android android-layout sms smsmanager

我正在创建此帖子以获取帮助。我正在开发一个发送incoming text sms的应用程序。我正在做的是获取incoming message body, date and time并将其作为新消息发送。出于发送目的,我正在使用sms manager。 我可以使用multiple message body获取checkboxes并创建list个所选邮件。但问题在于获取他们的日期和时间。

主要活动中的代码:

String body="";
ArrayAdapter<SMSListModel> adapter;
List<SMSListModel> list = new ArrayList<SMSListModel>();

所选消息的数组列表代码:

 private List<SMSListModel> getModel() 
{
    if(cursor.getCount()>0)
    {
        for(int i=0;i<cursor.getCount();i++)
        {
            if(cursor.moveToPosition(i))
            {
                list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))));
            }
        }
    }
    return list;
}

SMSListModel的代码

  public SMSListModel(String address, String body) {
    this.address = address;
    this.body = body;
}
public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

发送所选邮件正文的代码:

if(list.size()>0){
       for(int i=0;i<list.size();i++)
       {
           if(list.get(i).isSelected())
           {
            if(body.equals(""))
                   body =list.get(i).getBody();

               else
                body =list.get(i).getBody();
             try
             {
                 String mbody = "from"+ "dd/mm/yy" +"hh:mm"+body;
                 SmsManager smsManager = SmsManager.getDefault();
                 smsManager.sendTextMessage(phoneNo, null, mbody, null, null);
             }                           
             catch (Exception e)
             {
                 //Error Occurred if No Messages Selected 
                    e.printStackTrace();
             }

1 个答案:

答案 0 :(得分:1)

很少的代码仍然支持朋友,但我们可以修复您发布的代码,并为您指定现有代码,并且您没有在此处发布..

首先修改SMSListModel的代码

public class SMSListModel {

    String date;
    String time;
    String address;
    String body;

    public SMSListModel(String address, String body, String time, String date) {
        this.address = address;
        this.body = body;
        this.time = time;
        this.date = date;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
    public boolean isSelected() {
           return selected;
    }
    public void setSelected(boolean selected) {
    this.selected = selected;
    }

}

更改所选消息的数组列表的更新代码:

private List<SMSListModel> getModel() 
{
    if(cursor.getCount()>0)
    {
        for(int i=0;i<cursor.getCount();i++)
        {
            if(cursor.moveToPosition(i))
            {
                list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))),cursor.getColumnIndex("time")) , cursor.getColumnIndex("date")) );
            }
        }
    }
    return list;
}

注意:对于cursor.getColumnIndex(“time”))和cursor.getColumnIndex(“date”))首先需要在数据库类中创建两个名称time,date的列,并为其插入值当短信收到。

发送所选邮件正文的代码

if(list.size()>0){
       for(int i=0;i<list.size();i++)
       {
           if(list.get(i).isSelected())
           {
            if(body.equals(""))
                   body =list.get(i).getBody();
                   date =list.get(i).getDate();
                   time =list.get(i).getTime();

               else
                body =list.get(i).getBody();
                date =list.get(i).getDate();
                time =list.get(i).getTime();
             try
             {
                 String mbody = "from"+ date + time +body;
                 SmsManager smsManager = SmsManager.getDefault();
                 smsManager.sendTextMessage(phoneNo, null, mbody, null, null);
             }                           
             catch (Exception e)
             {
                 //Error Occurred if No Messages Selected 
                    e.printStackTrace();
             }

**注意:您需要修改数据库。对于该修改表,并在结束“时间”和“日期”添加两列。

之后,您还需要更改接收消息的broadcastReceiver子类的代码,并将此消息存储到数据库中。在该课程中,您还需要在表格中输入时间和日期值。**