内容观察者两次接听短信

时间:2013-09-26 07:34:46

标签: android onchange android-contentresolver contentobserver

您好我正在开发一个Android短信应用程序,我在使用ContentObserver来了解类似于此链接的传入消息

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

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

请帮助。谢谢!

1 个答案:

答案 0 :(得分:3)

它与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
}
相关问题