【问题标题】:Unable to fetch unread messages from SMS inbox无法从 SMS 收件箱中提取未读消息
【发布时间】:2022-08-18 15:17:15
【问题描述】:

我已成功检索收件箱中的所有邮件,但我只想要未读邮件。我还在查询中申请为read=0,它提供未读短信,但它不能检索未读消息,它检索所有消息。

    public ArrayList<Message> fetchInboxSms(int type)  {
        ArrayList<Message> smsInbox = new ArrayList<Message>();
          Uri uriSms = Uri.parse(\"content://sms\");
        SimpleDateFormat formatter1 = new SimpleDateFormat(\"dd/MM/yyyy\");
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(System.currentTimeMillis()-ONE_DAYS_MILIS);
        Date finaldate2 = calendar2.getTime();
        String date3 = formatter1.format(finaldate2);

        @SuppressLint(\"Recycle\") Cursor cursor = this.getContentResolver()
                .query(uriSms,
                        new String[] { \"_id\", \"address\", \"date\", \"body\",
                                \"type\", \" read = 0 \" }, \"type=\" + type, null,
                        \"date\" + \" COLLATE LOCALIZED ASC\");

        if (cursor != null) {
            cursor.moveToLast();
            if (cursor.getCount() > 0) {

                do {
                    Message message = new Message();
                  message.messageNumber = cursor.getString(cursor
                            .getColumnIndex(\"address\"));
                     //message.messageContent = cursor.getString(cursor.getColumnIndex(\"date\"));
                    String date =  cursor.getString(cursor.getColumnIndex(\"date\"));
                    String content = cursor.getString(cursor.getColumnIndex(\"body\"));             
                     
                    message.messageContent = content;
                     
                      smsInbox.add(message);
                } while (cursor.moveToPrevious());
            }
        }

        return smsInbox;

    }

    标签: java android android-sqlite


    【解决方案1】:

    像这样更改您的查询参数

    Cursor cursor = this.getContentResolver()
                .query(uriSms,
                    new String[]{"_id", "address", "date", "body",
                        "type"}, " read = 0 AND type = " + type, null,
                    "date" + " COLLATE LOCALIZED ASC");
    

    您在投影中传递 read = 0 的选择 where 子句而不是 where 子句。

    这就是您可以传递多个 where/selection 子句的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2019-08-24
      相关资源
      最近更新 更多