【问题标题】:content observer called twice for receiving sms内容观察者因接收短信而调用了两次
【发布时间】:2013-09-26 07:34:45
【问题描述】:

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

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

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

请帮忙。谢谢!

【问题讨论】:

    标签: android onchange android-contentresolver contentobserver


    【解决方案1】:

    它与 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2015-11-05
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多