【问题标题】:ACTION_TIME_CHANGED or ACTION_DATE_CHANGED, can't make them workACTION_TIME_CHANGED 或 ACTION_DATE_CHANGED,不能让它们工作
【发布时间】:2011-12-16 07:22:01
【问题描述】:

所以我正在努力争取一些为我工作的意图, 并且在 2 个特定意图上遇到问题。我搜索了这些组 和网络,似乎找不到比什么更多的信息 文档给了我。 我是否在 清单,或者如果我尝试在代码中注册接收器,我的接收器 永远不会收到ACTION_TIME_CHANGED 的操作或 ACTION_DATE_CHANGED。 据我所知,我应该在我的 每当用户明确设置时间/日期时,广播接收器 去"Settings" --> "Date & Time"然后修改时间或 日期 我是否误解了这些行动的目的? 我是否需要声明任何权限才能接收 这些动作? 有人参与过这个动作吗?

是否有一些使用此操作的有用示例,我找不到任何有用的东西,有很多格式更改示例,但没有实际 time_date 更改...

提前致谢

【问题讨论】:

    标签: android datetime date time


    【解决方案1】:

    除了<intent-filter>,请参阅Android ACTION_DATE_CHANGED broadcast 关于AOSP 错误:ACTION_TIME_CHANGED 应在时钟设置时触发,ACTION_DATE_CHANGED 应在时钟滴答到第二天时触发,但如果时钟设置倒退,ACTION_DATE_CHANGED 不会再次触发,直到时钟赶上第二天的时间。

    【讨论】:

    【解决方案2】:

    Intent.ACTION_DATE_CHANGEDIntent.ACTION_TIME_CHANGED仅在用户手动更改时间或日期时触发,系统不会自动触发。

    恐怕你必须使用Intent.ACTION_TIME_TICK,它是由系统每分钟准确触发的。

    【讨论】:

      【解决方案3】:

      是的,您必须添加

      <intent-filter>
         <action android:name="android.intent.action.DATE_CHANGED"></action>
      </intent-filter>
      

      【讨论】:

        【解决方案4】:

        在您的广播接收器 onReceive 方法中,我假设您具有与以下类似的设置?

        @Override
        public void onReceive(Context context, Intent intent){
            final String action = intent.getAction();
            if (Intent.ACTION_DATE_CHANGED.equals(action) || Intent.ACTION_TIME_CHANGED.equals(action)){
            // do stuff here
            }
            ...
        }
        

        另一个考虑因素是您是否向 IntentFilter 添加了适当的过滤器,例如...

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_DATE_CHANGED);
        

        【讨论】:

        • @Lukap 不需要权限。
        【解决方案5】:

        另一种方法,我过去常常在日期更改时采取行动。

        实际上,当您手动更改手机上的日期时,Intent.ACTION_DATE_CHANGED 仅适用于提前日期的日期,而不适用于之前的日期。此外,当您将时间设置为 11:59 并等待它变为 00:00 以向您提供日期更改的广播事件时,它不会给您触发。

        因此,我最终制定了自己的逻辑,将日期存储在共享首选项中,然后检查当前日期是否在先前存储的日期之后或之前(使用一些日期处理逻辑)并根据该日期更新我的数据。

          private void takesomeActionIfDateIsChanged() {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        
                long currentDateTimeInMillis = System.currentTimeMillis();
                Date currentDateWithMillis = new Date(currentDateTimeInMillis);
        
                String formattedCurrentDateWithOnlyDMYStr = dateFormat.format(currentDateWithMillis);
                Log.d(TAG, "formattedCurrentDate With Only DateMonthAndYear String : " + formattedCurrentDateWithOnlyDMYStr);
                Date formattedCurrentDate = dateFormat.parse(formattedCurrentDateWithOnlyDMYStr);
        
                String storedDateStr = mSpManager.getStoredDate(SPManager.STORED_DATE);
                Log.d(TAG, "storedDateStr : " + storedDateStr);
                Date storedDateOnlyWithDateMonthAndYear = dateFormat.parse(storedDateStr);
        
                if (formattedCurrentDate.after(storedDateOnlyWithDateMonthAndYear)) {
                    Log.d(TAG, "The current date is after stored date");
                    mSpManager.putNewDate(SPManager.STORED_DATE, formattedCurrentDateWithOnlyDMYStr);
                    mCounter = 0;
                } else if (formattedCurrentDate.before(storedDateOnlyWithDateMonthAndYear)) {
                    Log.d(TAG, "The current date is before stored date");
                    mSpManager.putNewDate(SPManager.STORED_DATE, formattedCurrentDateWithOnlyDMYStr);
                    mCounter = 0;
                } else {
                    Log.d(TAG, "The current date is same as stored date");
                }
            } catch (Exception e) {
                Log.e(TAG, "exception : ", e);
            }
        }
        

        【讨论】:

        • Date 类已被弃用,并且不需要格式化日期来检查值,因此您可以节省一些 CPU。您应该考虑使用Calendar 甚至更好:LocalDate(通过库可用于旧的 Android 版本:github.com/JakeWharton/ThreeTenABP)。 LocalDate 在内存和 CPU 方面效率更高。这样,可以很容易地检查当前日期是否与前一个日期不同,如果您使用 Kotlin,您甚至可以使用普通的比较符号(例如 != )。
        • @Richa,当您将时间设置为 11:59 并等待它变为 00:00 给您日期更改的广播事件时,它不会给您触发. -> 这对我有用。
        【解决方案6】:

        您不需要任何许可。

        为了收到有关日期更改的通知,您需要使用这些操作(以及 ACTION_TIMEZONE_CHANGED)注册到 BroadcastReceiver

        这是我制定的解决方案:

        https://stackoverflow.com/a/48782963/878126

        【讨论】:

          猜你喜欢
          • 2012-12-18
          • 2012-09-23
          • 2019-02-04
          • 1970-01-01
          • 2015-08-01
          • 1970-01-01
          • 2011-09-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多