redrom
redrom

Reputation: 11642

Android : How to get only last sent sms in outbox?

Trying to get last SMS from content provider using this way.

@Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Uri uriSMSURI = Uri.parse("content://sms/");
        String selection = "type = '2'"; //Selection parameter to only select messages in sent folder
        // save the message to the SD card here
        Logger.d("On Change");
        Toast.makeText(mCtx,"TEST", Toast.LENGTH_LONG).show();
        Cursor cur = mCtx.getContentResolver().query(uriSMSURI, null, selection,
                null, null);
        String body = null;

        if(cur.moveToFirst()){
            body = cur.getString(cur.getColumnIndexOrThrow("body")).toString();
            Logger.d(body);
            cur.close();
        }

But i always getting last TWO MESSAGES FROM OUTBOX, but i want the only last.

How should i do it in the right way please?

EDIT:

Log after send message with text "TWO":

01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ╔════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ║ Thread: main
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ║ ContentObserver.onChange  (ContentObserver.java:129)
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ║    SMSoutObserver.onChange  (SMSoutObserver.java:46)
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ║ On Change
01-09 16:10:27.205 4230-4230/? D/PRETTYLOGGER: ╚════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ╔════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ║ Thread: main
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ║ ContentObserver.onChange  (ContentObserver.java:129)
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ║    SMSoutObserver.onChange  (SMSoutObserver.java:54)
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ║ ONE
01-09 16:10:27.245 4230-4230/? D/PRETTYLOGGER: ╚════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.265 1685-1685/? V/SmsReceiverService: onStart: #1 mResultCode: -1 = Activity.RESULT_OK
01-09 16:10:27.305 4230-4230/? D/PRETTYLOGGER: ╔════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.315 4230-4230/? D/PRETTYLOGGER: ║ Thread: main
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ║ ContentObserver.onChange  (ContentObserver.java:129)
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ║    SMSoutObserver.onChange  (SMSoutObserver.java:46)
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ║ On Change
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ╚════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ╔════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ║ Thread: main
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ║ ContentObserver.onChange  (ContentObserver.java:129)
01-09 16:10:27.325 4230-4230/? D/PRETTYLOGGER: ║    SMSoutObserver.onChange  (SMSoutObserver.java:54)
01-09 16:10:27.335 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.335 4230-4230/? D/PRETTYLOGGER: ║ ONE
01-09 16:10:27.335 4230-4230/? D/PRETTYLOGGER: ╚════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.335 1685-1685/? V/SmsReceiverService: onStart: #1 mResultCode: -1 = Activity.RESULT_OK
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╔════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║ Thread: main
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║ ContentObserver.onChange  (ContentObserver.java:129)
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║    SMSoutObserver.onChange  (SMSoutObserver.java:46)
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║ On Change
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╚════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╔════════════════════════════════════════════════════════════════════════════════════════
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║ Thread: main
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║ ContentObserver.onChange  (ContentObserver.java:129)
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║    SMSoutObserver.onChange  (SMSoutObserver.java:54)
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╟────────────────────────────────────────────────────────────────────────────────────────
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ║ TWO
01-09 16:10:27.355 4230-4230/? D/PRETTYLOGGER: ╚════════════════════════════════════════════════════════════════════════════════════════

Upvotes: 0

Views: 723

Answers (2)

Mike M.
Mike M.

Reputation: 39191

You can set the last argument in the query() call to "date DESC LIMIT 1", but this might not work everywhere.

I misunderstood the question, which was resolved in comments, so I'll edit the relevant portions into the answer for reference.

When an SMS message is sent using the platform SMS app - as well as many third-party SMS clients - it goes through a few steps that involve "moving" the message around in the Provider. First it gets written to the queue, then moved to the outbox, and finally to the sent box (if successful). The Provider does this by changing the value of the message's type column, and your Observer's onChange() method is going to fire each time that happens. Since the posted code is querying for only sent messages, the first two times onChange() fires, the previously sent message is returned, rather than the current message, as the current will have type 6, and then 4 for the first two "moves".

The solution is to change the query to omit the selection by passing null, which will cause the first message in the returned Cursor to be the last message updated. We then check the type column for a value of 2, indicating that the message has been successfully sent, and we can then process it as needed.

Upvotes: 1

redrom
redrom

Reputation: 11642

Resolved using :

Cursor cur = mCtx.getContentResolver().query(uriSMSURI, null, null,
                null, "date DESC LIMIT 1");
        String body = null;

        if(cur.moveToFirst()){
            String messageType = cur.getString(cur.getColumnIndexOrThrow("type")).toString();
            if(messageType.equals("2")) { //2 == type sent
                String type = cur.getString(cur.getColumnIndexOrThrow("type")).toString();
                body = cur.getString(cur.getColumnIndexOrThrow("body")).toString();
                Logger.d(body);
                Logger.d(type);
            }

        }
        cur.close();

Thanks to Mike for his advice.

Upvotes: 0

Related Questions