【问题标题】:Sent SMS observer has double the output发送短信观察者有双倍的输出
【发布时间】:2014-08-11 13:12:12
【问题描述】:

我正在尝试使用ContentObserver 为外发消息创建 SMS 观察器。下面的代码运行良好,但每当我通过向同事发送短信来测试时,我都会得到两次输出。

观察者在服务中注册,如下所示:

SentSMSObserver sentSMSObserver = new SentSMSObserver(new Handler(), this);
getContentResolver().registerContentObserver(sentSMSObserver.CONTENT_SMS_URI, true, sentSMSObserver);

每当我向自己的号码发送短信时,我只会得到一次输出,这真的很奇怪。由于服务是单例的(据我的研究让我相信),我的观察者不太可能有第二个实例。

public class SentSMSObserver extends ContentObserver {

    private static final String CONTENT_SMS = "content://sms";
    public final Uri CONTENT_SMS_URI = Uri.parse(CONTENT_SMS);
    private Context context;

    public SentSMSObserver(Handler handler, Context context) {
        super(handler);
        this.context = context;
    }

    @Override
    public void onChange(boolean selfChange) {
        Cursor cursor = context.getContentResolver().query(CONTENT_SMS_URI, null, null, null, null);
        try {
            if (cursor.moveToNext()) {
                String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
                int type = cursor.getInt(cursor.getColumnIndex("type"));
                if (protocol != null || type != Telephony.TextBasedSmsColumns.MESSAGE_TYPE_SENT) {
                    return;
                }

                String to = cursor.getString(cursor.getColumnIndex("address"));
                Date now = new Date(cursor.getLong(cursor.getColumnIndex("date")));
                String message = cursor.getString(cursor.getColumnIndex("body"));
                Log.e("sentmessage", to + " - " + now + " - " + message);
            }
        } finally {
            cursor.close();
        }
    }
}

Logcat:

08-11 14:25:14.292  12574-12574/com.androidfun.smstest E/sentmessage﹕ +(deleted phone n°) - Mon Aug 11 14:25:10 CEST 2014 - Test
08-11 14:25:18.306  12574-12574/com.androidfun.smstest E/sentmessage﹕ +(deleted phone n°) - Mon Aug 11 14:25:10 CEST 2014 - Test

【问题讨论】:

    标签: android android-service contentobserver smsmanager


    【解决方案1】:

    在某些情况下,发送的消息也会更新为“发送确认”,甚至是“发送确认”。将其发送给自己,您可能不会收到“已发送确认”,因为它是一步发送和确认的。

    在此处查看帖子:

    SMS sent observer executes 3 times

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多