哪些有效的SMS字段?

时间:2018-09-25 16:22:17

标签: android sms

我希望有一个接收方,以便在收到SMS消息后立即阅读它们。周围有很多示例接收器,构建一个接收器并不需要太多工作。知道了。

另一个要求是向Android的内容提供商查询SMS。我写这样的东西来查询短信:

public static List<SMSMessage> getAllSMSMessages(@NonNull Context context){

        List<SMSMessage> smsMessageList = null;

        Cursor smsCursor = null;

        Uri contentUri;

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            contentUri = Telephony.Sms.CONTENT_URI;
        }
        else{
            contentUri = Uri.parse("content://sms");
        }

        try{
            smsCursor = context.getContentResolver().query(contentUri
                    , null, null, null,
                    "date DESC");

            if(smsCursor != null && smsCursor.moveToFirst()){

                smsMessageList = new ArrayList<SMSMessage>(smsCursor.getCount());

                // Prepare the column names
                String columnId, columnMessageType, columnAddress, columnMessageBody, columnMessageReadState,
                        columnMessageReceivedDateTime, columnMessageSentDateTime, columnMessageStatus;

                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                    columnId = Telephony.Sms._ID;
                    columnMessageType = Telephony.Sms.TYPE;
                    columnAddress = Telephony.Sms.ADDRESS;
                    columnMessageBody = Telephony.Sms.BODY;
                    columnMessageReadState = Telephony.Sms.READ;
                    columnMessageReceivedDateTime = Telephony.Sms.DATE;
                    columnMessageSentDateTime = Telephony.Sms.DATE_SENT;
                    columnMessageStatus = Telephony.Sms.STATUS;
                }
                else{
                    columnId = "_id";
                    columnMessageType = "type";
                    columnAddress = "address";
                    columnMessageBody = "body";
                    columnMessageReadState = "read";
                    columnMessageReceivedDateTime = "date";
                    columnMessageSentDateTime = "date_sent";
                    columnMessageStatus = "status";
                }

                do{
                    int id = Integer.parseInt(smsCursor.getString(smsCursor.getColumnIndex(columnId)));
                    Integer messageType = Integer.parseInt(smsCursor.getString(smsCursor.getColumnIndex(columnMessageType)));
                    String address = smsCursor.getString(smsCursor.getColumnIndex(columnAddress));
                    String body = smsCursor.getString(smsCursor.getColumnIndex(columnMessageBody));
                    String readState = smsCursor.getString(smsCursor.getColumnIndex(columnMessageReadState));
                    long receivedDateTime = Long.parseLong(smsCursor.getString(smsCursor
                            .getColumnIndex(columnMessageReceivedDateTime)));
                    long sentDateTime = Long.parseLong(smsCursor.getString(smsCursor
                            .getColumnIndex(columnMessageSentDateTime)));
                    String messageStatus = toAppDefinedMessageStatus(Integer.parseInt(smsCursor.getString(smsCursor.getColumnIndex(columnMessageStatus))));

                    SMSMessage message = new SMSMessage(Long.toString(System.currentTimeMillis()), id, toAppDefinedMessageType(messageType), address, body, readState,
                            receivedDateTime, sentDateTime, messageStatus);

                    smsMessageList.add(message);

                }while (smsCursor.moveToNext());
            }
        }finally {
            if(smsCursor != null){
                smsCursor.close();
            }
        }

        return smsMessageList;
    }

此方法成功返回提供者的所有消息。现在,正如您所看到的,我已经对具有上面查询的字段的SMS消息进行了建模。无论我在哪里构造消息,我都希望保持相同的模型-无论是内容提供商返回的消息还是我从接收方收到的消息。

除了...我无法构造内容提供程序通过接收方解析消息时提供的所有字段。

如果您决定搜索,则有一种方法可以解析来自Intent捆绑包的消息:

https://developer.android.com/reference/android/telephony/SmsMessage.html#createFromPdu(byte[],%20java.lang.String)

但是,此方法有助于仅包含基本字段的消息。在这种情况下,如何检索其他字段(例如通过提供者)?

0 个答案:

没有答案