Why my ContentObserver is not being called when I send an SMS?

点点圈 提交于 2019-11-29 11:24:54

If you replace content://sms/out by content://sms/, you'll see that the ContentObserver is not called when the SMS is sent but when one is received. Here is the code of my observer. It only enters onChange(boolean) when receiving a SMS. The protocol is "0" at that time.

So, I am struck at the same point as you :(

class SmsProviderObserver extends ContentObserver {

    public SmsProviderObserver(Handler handler) {
        super(handler);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        Uri uriSms = Uri.parse("content://sms/");

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(uriSms, null, null, null, null);
        // this will make it point to the first record, which is the last
        // SMS sent
        if (!cur.moveToNext()) {
            return; // weird!
        }

        String protocol = cur.getString(cur.getColumnIndex("protocol"));
        if (protocol == null) {
            // send
            Log.i("SMS", "SMS SEND");
            int threadId = cur.getInt(cur.getColumnIndex("thread_id"));

            Log.i("SMS", "SMS SEND ID = " + threadId);
            Cursor c = cr.query(
                    Uri.parse("content://sms/outbox/" + threadId), null,
                    null, null, null);
            c.moveToNext();
            int p = cur.getInt(cur.getColumnIndex("person"));
            Log.i("SMS", "SMS SEND person= " + p);
            // getContentResolver().delete(Uri.parse("content://sms/conversations/"
            // + threadId), null, null);

        } else {
            // receive
            Log.i("SMS", "SMS RECIEVE");
            int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));

            cr.delete(
                    Uri.parse("content://sms/conversations/" + threadIdIn),
                    null, null);
        }
    }
}

And the code registering the observer:

ContentResolver contentResolver = getContentResolver();
mSmsObserverHandler = new Handler();
mSmsProviderObserver = new SmsProviderObserver(mSmsObserverHandler);
contentResolver.registerContentObserver(Uri.parse("content://sms/"), true, mSmsProviderObserver);
Andres

Ok, seems there is no way, don´t know if it is my error on an Android bug but I got it working:

        if( (type == 2 || type == 1) && (!lastID.contentEquals(cur.getString(cur.getColumnIndex("_id")))) ){
            String protocol = cur.getString(cur.getColumnIndex("protocol"));
            lastID = cur.getString(cur.getColumnIndex("_id"));
            // Mensaje enviado
            if(protocol == null){
                Log.i("SMSStatus", "SMS Sent");
            }
            // Mensaje recibido
            else{
                Log.i("SMSStatus", "SMS received");
            }
        }

When an SMS is received it is type = 1, when send an SMS and its type 6, 4 and finally when it is sent is type = 2. Be carefull because sometimes the onChange method is called lots of times no matters if it is only one SMS so you should detect if it is the same SMS by checking the _id parameter.

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