【问题标题】:How to fetch multiple text message details?如何获取多条短信详细信息?
【发布时间】:2014-02-25 18:29:42
【问题描述】:

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

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

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

发送选定消息正文的代码:

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

看一下,如果可能的话,请给我适当的修改

【问题讨论】:

  • 尝试根据我的回答获取日期和时间
  • @MD 但是你的答案在哪里?
  • 我删除了。我认为它不起作用
  • @MD 你会再更新答案吗

标签: android sms smsmanager


【解决方案1】:

嘿试试这样:

  ArrayList<Object> time = new ArrayList<Object>();

    Uri uri = Uri.parse("content://sms/inbox");
    c = getContentResolver().query(uri, null, null, null, null);
    startManagingCursor(c);

    // Read the sms data and store it in the list
    if (c.getCount() > 0) {
        if (c.moveToFirst()) {
            for (int i = 0; i < c.getCount(); i++) {
                String date = c.getString(c.getColumnIndex("date"));
                Long timestamp = Long.parseLong(date);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(timestamp);
                DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                String messageDate = formatter.format(calendar.getTime());
                time.add(messageDate);  
                c.moveToNext();
            }
        }

更新:并更改Date format 喜欢:

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

String strBody="from "+time.get(position)+body;

【讨论】:

  • @M D 我想获取收到的短信日期和时间,并用它来编写新消息的正文
  • @Androidbeginner 在这里,我将 SMS 日期和显示添加到我的 ListView 中。就是这样
  • @M D 我的问题是我希望在新邮件正文中使用所选短信的日期和时间String mbody
  • @Androidbeginner 那么这很简单。将您的消息日期转换为字符串并附加到带有正文的字符串中。
  • @MD 这就是我需要的日期和时间。你能告诉我怎么做吗
【解决方案2】:

您需要在模型中定义另一个成员。这是您如何读取日期并通过构造函数/设置器将其设置为模型的方法。

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

String date =  cursor.getString(cursor.getColumnIndex("date"));
    Long timestamp = Long.parseLong(date);    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    Date finaldate = calendar.getTime();
    String smsDate = finaldate.toString();

model.setSMSDate(smsDate);
                list.add(model);
            }
        }
    }
    return list;
}

在您的发送短信代码逻辑中,只需从模型中读取它并将其添加到您的短信正文中

list.get(i).getSMSDate();

尚未对此进行测试,但您明白了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多