Android ContentObserver- determine the SMS status

柔情痞子 提交于 2019-12-24 11:36:08

问题


I've registered a ContentObserver to the URI of Telephony.Sms.CONTENT_URI, in order to be notified every time the user sends or receives a new SMS message.

For successfully-sent and received messages, this works fine - but for messages that have failed to send, I noticed an unexpected behavior: The messages seem to be reported as if they were successfully sent, even though they're not - and I couldn't find any indication about the failure.

Inside the obeserver's onChange function, I tried to determine the message status according to its type, but all I get is folder/type of Telephony.Sms.MESSAGE_TYPE_SENT (2), and status of Telephony.Sms.STATUS_NONE (-1), both of which aren't indicative that the sending has failed. That's the code I used:

@Override
public void onChange(boolean selfChange, Uri uri) {
    ContentResolver contentResolver = mContext.getContentResolver();
    try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
        if (cursor == null || !cursor.moveToFirst()) {
            // log goes here
            return;
        }

        int folderId = cursor.getInt(cursor.getColumnIndex("type"));
        int status = cursor.getInt(cursor.getColumnIndex("status"));
    }
}

I also tried, within the same function, to query (using the URI of Telephony.Sms.CONTENT_URI) for all the SMS messages which I haven't seen before, and then extracting the same fields like in previous code example - but the result would stay the same.

I've also tried to query for specific URIs that should return list of all messages with a specific status (as listed here) but I kept on getting an empty cursor, not sure why:

Cursor cursor = contentResolver.query(Telephony.Sms.CONTENT_URI.buildUpon().appendPath("all").build(), null, null, null, null))

(also tried with other appended paths, such as "failed", "queued", "outbox", "undelivered", all of which didn't work).

By the way, this code has been tested only on Samsung Galaxy A7, running Android 7.0, when the default messaging app is Google's Android Messages (not the Samsung messaging app that was bundled with the phone), so maybe it somehow affected the results, I'm not sure.

Bottom line, the question is - how can I check if the message has been successfully sent or not? (assuming that I'm not the one who's sending it, because then I'll get a callback which contains the status for that specific attempt)

来源:https://stackoverflow.com/questions/52205463/android-contentobserver-determine-the-sms-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!