无法从特定设备(MI设备)上的收件箱中读取所有SMS

时间:2017-03-07 04:21:48

标签: android

我的收件箱包含约。 2032短信,但这个查询只返回206短信。总短信:7024

 Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

        while (cur.moveToNext()) {
               String address = cur.getString(cur.getColumnIndex("address"));
               String body = cur.getString(cur.getColumnIndexOrThrow("body"));
              sms.add("Number: " + address + " .Message: " + body);  

          }
        return sms;

1 个答案:

答案 0 :(得分:1)

从API 19开始,您可以使用Telephony Class;由于硬编码值不会检索每个设备中的消息,因为内容提供商Uri从设备和制造商处更改。

public void getAllSms(Context context) {

    ContentResolver cr = context.getContentResolver();
    Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
    int totalSMS = 0;
    if (c != null) {
        totalSMS = c.getCount();
        if (c.moveToFirst()) {
            for (int j = 0; j < totalSMS; j++) {
                String smsDate = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.DATE));
                String number = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.ADDRESS));
                String body = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.BODY));
                Date dateFormat = new Date(Long.valueOf(smsDate));
                String type = null;
                switch (Integer.parseInt(c.getString(c.getColumnIndexOrThrow(Telephony.Sms.TYPE)))) {
                    case Telephony.Sms.MESSAGE_TYPE_INBOX:
                        type = "inbox";
                        break;
                    case Telephony.Sms.MESSAGE_TYPE_SENT:
                        type = "sent";
                        break;
                    case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
                        type = "outbox";
                        break;
                    default:
                        break;
                }
                if (type != null) {
                    Toast.makeText(context, "Message present", Toast.LENGTH_SHORT).show();
                }

                c.moveToNext();
            }
        }
    } else {
        Toast.makeText(this, "No message to show!", Toast.LENGTH_SHORT).show();
    }
}