ACTION_TIME_CHANGED 或 ACTION_DATE_CHANGED,无法使它们工作

发布于 2024-12-12 04:31:47 字数 431 浏览 0 评论 0原文

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

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

提前致谢

So I am in the process of trying to get a few intents to work for me,
and am having problems on 2 specific intents. I searched these groups
and the webs and can 't seem to find any more information than what
the documentation gives me.
It doesn't seem to matter if I declare the use of these intent in the
manifest, or if I try to register a receiver in code, my receiver
never receives the actions for ACTION_TIME_CHANGED or
ACTION_DATE_CHANGED.
As far as I can tell I should be receiving these actions in my
broadcast receiver whenever the user explicitly sets the time/date by
going to "Settings" --> "Date & Time" and then modifying the time or
date
Am I mis-interpreting what these actions are for?
Are there any permissions that I need to declare in order to receive
these actions?
Does anybody worked with this actions ?

Is there some useful example where this actions are used, I could not find anything useful, there is plenty of examples of format change but not on actual time_date change...

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦醒时光 2024-12-19 04:31:48

是的,您必须添加

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

Yes, you must add

<intent-filter>
   <action android:name="android.intent.action.DATE_CHANGED"></action>
</intent-filter>
左岸枫 2024-12-19 04:31:48

你不需要任何许可。

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

这是我提出的解决方案:

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

You don't need any permission.

In order to be notified of date changes, you need to register to a BroadcastReceiver with those actions (and also ACTION_TIMEZONE_CHANGED) .

Here's a solution I've made:

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

若水微香 2024-12-19 04:31:48

在您的广播接收器 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);

Inside your broadcast receiver onReceive method I am assuming u have a similar setup to the following?

@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
    }
    ...
}

Another consideration would be did you add the proper filters to your IntentFilter such as...

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_DATE_CHANGED);
少女的英雄梦 2024-12-19 04:31:48

我用来在日期更改时采取操作的另一种方法。

实际上,当您手动更改手机上的日期时,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);
    }
}

An alternative approach which I used to take an action when the date is changed.

Actually, the Intent.ACTION_DATE_CHANGED only works with the date of an advanced date and not with a previous date when you change the date on the phone manually. Also, when you set time to 11:59 and wait for it to become 00:00 to give you the broadcast event of the date change, it doesn't give you the trigger.

So, I ended up making my own logic of storing a date in shared pref and then checking if the current date is after or before previously stored date (with some date processing logic) and updating my data based on that.

  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);
    }
}
顾忌 2024-12-19 04:31:47

除了 之外,请参阅 Android关于 AOSP 错误的 ACTION_DATE_CHANGED 广播ACTION_TIME_CHANGED 应在时钟设置后触发,并且当时钟滴答作响到第二天时,ACTION_DATE_CHANGED 应该触发,但如果时钟向后设置,ACTION_DATE_CHANGED 不会再次触发,直到时钟赶上正在发生的事情到第二天。

In addition to the <intent-filter>, see Android ACTION_DATE_CHANGED broadcast about an AOSP bug: ACTION_TIME_CHANGED should fire when the clock gets set and ACTION_DATE_CHANGED should fire when the clock ticks over to the next day, but if the clock gets set backwards, ACTION_DATE_CHANGED won't fire again until the clock catches up with what was going to be the next day.

三生殊途 2024-12-19 04:31:47

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

恐怕你必须使用 Intent.ACTION_TIME_TICK,它由系统每分钟触发一次。

Intent.ACTION_DATE_CHANGED and Intent.ACTION_TIME_CHANGED are triggered only when the user manually changes time or date, and they are not automatically triggered by the system.

I'm afraid you have to use Intent.ACTION_TIME_TICK, which is triggered by the system exactly each minute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文