短信ContentObserver的onChange()触发多次 [英] Sms ContentObserver onChange() fires multiple times

查看:1009
本文介绍了短信ContentObserver的onChange()触发多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了多次,但没有人能够拿出从我所看到的一个工作的答案。

I know this question has been asked multiple times, but nobody has been able to come up with a working answer from what I have seen.

林工作的一个应用程序来拦截短信并根据发送#,弹出一个自定义警报。我有它的工作精美,广播接收器,但是如果用户已经goSms安装了的onReceive()方法不会被调用为goSms中止之前它曾经到达我的应用程序。

Im working on an app to intercept text messages and depending on the sending #, pop up with a custom alert. I have it working beautifully with a broadcast receiver, however if the user has goSms installed the onReceive() method is never called as goSms aborts it before it ever reaches my app.

要解决这个问题,我试着在内容的内容观察员://短信/ 它的工作就好了,但是的onChange()被调用了两次,用完全相同的参数。伊夫尝试检查的时间标记,但它们是相同的,为的是的类型和予有设置每隔参数

To get around this, Im trying a content observer on content://sms/ Its working just fine, however the onChange() is called twice, with exactly the same parameters. Ive tried to check the time stamps, but they are the same, as is the type and every other parameter I have set.

这是我所看到的,这是一个常见的​​问题,但没有一个是我见过的任何地方接听。

From what I've seen, this is a common issue, but not one that I've seen answered anywhere.

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

protected void querySMS() {
    Cursor cur = getContentResolver().query(u, null, null, null, null);
    cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent
    String type = cur.getString(cur.getColumnIndex("type"));
    String body = cur.getString(cur.getColumnIndex("body")); //content of sms
    String add = cur.getString(cur.getColumnIndex("address")); //phone num
    if (type.equals("1")) {
        if (add.equals(Test.SENDER)) {              
            String[] bodys = body.split(" ", 7);
            if (bodys[0].equals("test")) {
                test = true;
            }
            cat = bodys[1];
            level = bodys[2];
            urgency = bodys[3];
            certainty = bodys[4];
            carrier = bodys[5];
            message = bodys[6];
            final Intent intent = new Intent(context, AlertActivity.class);
            Bundle b = new Bundle();
            b.putString("title", cat);
            b.putString("certainty", certainty);
            b.putString("urgency", urgency);
            b.putString("level", level);
            b.putString("message", message);
            b.putBoolean("test", test);
            intent.putExtras(b);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            carrierName = manager.getNetworkOperatorName();
            if (carrierName.replaceAll(" ", "").equals(carrier)) {

                context.startActivity(intent);
            } else {
                //testing
                Toast.makeText(context, carrierName.replaceAll(" ", ""), Toast.LENGTH_LONG).show();
            }
        }
    }
}

由于

的onChange()被解雇了两次,林获得两个警报,以及。我不能为我的生活想出一个办法解决这个问题。

Because of the onChange() being fired twice, Im getting two alerts as well. I cannot for the life of me figure out a way around this.

推荐答案

如果两者是相同的:
存储每封邮件recv'd
把它比作previous消息recv'd
如果没有找到,方法
如果找到,则丢弃该消息

If the two are identical:
store each message recv'd
compare it to previous messages recv'd
if not found, process
if found, discard the message

存储应该是无穷的消息的生活,5条消息有点循环缓冲区应该就可以了。

The life of the messages stored should be infinitesimal, a little circular buffer of 5 messages should be fine.

这篇关于短信ContentObserver的onChange()触发多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆