Need to detect SMS Sent in android

618 views Asked by At

I know this question is way old and may be duplicate, but I have a particular query based on it for which I am still trying to find the solution.

As far as I know, ContentObserver on URI content://sms will be triggered when ever there is change in data. I have tried,

https://katharnavas.wordpress.com/2012/01/18/listening-for-outgoing-sms-or-send-sms-in-android/

Android : Catching Outgoing SMS using ContentObserver or receiver not working

and many more...

As per my understanding this can only be achieved by Content provider and there is no broadcast to handle this. Also content://sms/sent doesn't work at

contentResolver.registerContentObserver(Uri.parse("content://sms/sent"), true, observer);

Am receiving the onChange for each messaging data whether its SMS sent, received, deleted. So as per the documentation of android and other stackoverflow links I found using https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns.html#MESSAGE_TYPE_SENT as comparison method.

So my check goes as,

@Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Uri uriSMSURI = Uri.parse("content://sms");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
        cur.moveToFirst();
        String id = cur.getString(cur.getColumnIndex("_id"));
        String protocol = cur.getString(cur.getColumnIndex("protocol"));
        Log.d(TAG, "protocol = " + protocol);
        int type = cur.getInt(cur.getColumnIndex("type"));
        Log.d(TAG, "onChange: type = " + type);
        // I have even tried with protocol == null check.
        if (type == 2 && smsChecker(id)) {
            Log.d(TAG, "Sent sms");
        }
        cur.close();
    }

    private boolean smsChecker(String smsId) {
        boolean flagSMS = true;

        if (smsId.equals(lastSmsId)) {
            flagSMS = false;
        }
        else {
            lastSmsId = smsId;
        }

        return flagSMS;
    }

So my question here is, If I delete any messages or trigger a outgoing message I receive type 2 on my HTC phone - L OS (sometimes for incoming message too I see type==2).

How can I separate this onChange request, as I am particularly looking for SMS SENT change.

0

There are 0 answers