内容观察者调用两次以接收短信



嗨,我正在开发一个安卓短信应用程序,其中我正在使用 ContentObserver 来了解类似于此链接的传入消息

http://rdcworld-android.blogspot.in/2011/10/listen-sms-mms-programmatically-android.html

我需要获取收到的短信计数。但是,ContentObserver onChange 方法被调用了两次,我无法获得收到的短信的正确计数。我该如何解决这个问题

请帮忙,谢谢!

它与 SMS 何时传入、触发,然后在与底层数据库同步时再次触发有关。 我找到的最佳解决方案,实现一种忽略第二个调用的方法:

Long theDT = System.currentTimeMillis();
Long nextAM = Long.valueOf(1000);  //Use a 1 second minimum delay to avoid repeated calls
Long lastAM = preferences.getLong("lastAM", 0);
if ((lastAM + nextAM) < theDT1){
    SharedPreferences.Editor editor = preferences.edit();
    editor.putLong("lastAM", theDT); // value to store
    editor.commit();
    // DO WHAT YOU NEED TO DO HERE
}

最新更新