且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我发送短信时我的 ContentObserver 没有被调用?

更新时间:2022-11-24 08:39:28

好吧,似乎没有办法,不知道是不是我的错误在 Android 上的错误,但我得到了它:

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");
            }
        }

当收到一条短信时,它是 type = 1,当发送一条短信时,它的类型是 6、4,最后当它被发送时是 type = 2.要小心,因为有时 onChange 方法被调用了很多次,不管是否它只是一条短信,因此您应该通过检查 _id 参数来检测它是否是同一条短信.

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.