每隔几秒钟就会收到TIMEZONE_CHANGED意图

时间:2021-02-26 01:24:39

I use a BroadcastReceiver with TIMEZONE_CHANGED action to reset alarms using AlarmManager just to make sure that the alarm runs exactly at the set time, not a few hours earlier or later, depending on the time zone change.

我使用带有TIMEZONE_CHANGED动作的BroadcastReceiver来使用AlarmManager重置警报,只是为了确保警报在设定的时间运行,而不是在几个小时之前或之后,这取决于时区的更改。

However in the latest log sent by the user, I saw information about intent with TIMEZONE_CHANGED action being received every few seconds with the user complaining about app being glitchy.

然而,在用户发送的最新日志中,我看到每隔几秒钟就会收到关于TIMEZONE_CHANGED操作意图的信息,用户抱怨应用程序不稳定。

Here's my BroadcastReceiver's onReceive code

这是我的广播接收器的onReceive代码

@Override
public void onReceive(Context context, Intent intent) {
    Utils.log("OnTimeChange");
    String action = intent.getAction();

    if (action.equals(Intent.ACTION_TIME_CHANGED)) {
        Utils.log("TimeChange");
    } else if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
        Utils.log("TimeZoneChanged");
    }
    BroadcastsManager.updateBroadcastsFromAlarms(context,
            AlarmsDbAdapter.getInstance(context));
}

The manifest's intent filter:

清单的意图过滤器:

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

And part of the log (it goes like that for over an hour - full lenght of the log)

还有部分日志(像这样写了一个多小时——全是日志)

1. 19/4 7:41:54 - posting alarm 3 for 8:15 (in 0h)
2. 19/4 7:44:29 - OnTimeChange
3. 19/4 7:44:29 - TimeZoneChanged
4. 19/4 7:44:29 - posting alarm 3 for 8:15 (in 0h)
5. 19/4 7:44:54 - OnTimeChange
6. 19/4 7:44:54 - TimeChange
7. 19/4 7:44:54 - posting alarm 3 for 8:15 (in 0h)

It's a Samsung Galaxy S III (Android v 4.1.2). The strange thing is, that doesn't happen on my S III. Could it be that the user has set "automatic timezone change by provider" option enabled on his/her device and information like that is sent every few seconds?

这是三星Galaxy S III (Android v 4.1.2)。奇怪的是,这不会发生在我的S III上。是否有可能用户已经设置了“由提供者提供的自动时区更改”选项,并且每隔几秒就会发送这样的信息?

Has anyone expierienced it? I guess I will just add an option to check if timezone has actually changed before updating broadcasts, but it is still getting the receiver called every few seconds...

有人expierienced吗?我想我只需要添加一个选项,在更新广播之前检查时区是否真的改变了,但是它仍然每隔几秒钟就会被调用一次……

2 个解决方案

#1


8  

I still don't know why timezone change and time set are called so often, but I was able to work out a solution, that lets me find out when reaction is actually needed.

我仍然不知道为什么时区变化和时间设置经常被调用,但是我能够找到一个解决方案,让我知道什么时候需要反应。

Also I am listening only to timezone changes now.

我现在只听时区的变化。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

String oldTimezone = prefs.getString(PREF_TIMEZONE, null);
String newTimezone = TimeZone.getDefault().getID();

long now = System.currentTimeMillis();

if (oldTimezone == null || TimeZone.getTimeZone(oldTimezone).getOffset(now) != TimeZone.getTimeZone(newTimezone).getOffset(now)) {
     prefs.edit().putString(PREF_TIMEZONE, newTimezone).commit();
     Logger.log("TimeZone time change");
    //update alarms
}

I added zone's time check, because very often I found out, that althought zones change, they do not differ in time in any way. Also some users claimed that they didn't even travel anywhere far when numerous changes in zones were detected - just regular trips to work and back.

我增加了区域的时间检查,因为我经常发现,所有的区域都在变化,它们在时间上没有任何不同。还有一些用户声称,当检测到许多区域的变化时,他们甚至没有去任何很远的地方——只是定期往返上班。

Check limited number of unneeded operations.

检查有限数量的不必要的操作。

#2


0  

Avoid listening for:

避免监听:

<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />

it seems they get called regularly also when time has not been set nor timezone changed. I suspect it is linked to the user using "use network provided time zone and time" in the phone's settings.

似乎他们在没有设定时间和时区改变的时候也会被定期的呼叫。我怀疑它与用户在手机设置中使用“使用网络提供的时区和时间”有关。

If you really do need to listen for these broadcasts you should check if time really has changed significantly or if its just a millisecond correction from the network provided time

如果你真的需要收听这些广播,你应该检查时间是否真的发生了显著的变化,或者它是否仅仅是网络提供的时间的一毫秒的修正

#1


8  

I still don't know why timezone change and time set are called so often, but I was able to work out a solution, that lets me find out when reaction is actually needed.

我仍然不知道为什么时区变化和时间设置经常被调用,但是我能够找到一个解决方案,让我知道什么时候需要反应。

Also I am listening only to timezone changes now.

我现在只听时区的变化。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

String oldTimezone = prefs.getString(PREF_TIMEZONE, null);
String newTimezone = TimeZone.getDefault().getID();

long now = System.currentTimeMillis();

if (oldTimezone == null || TimeZone.getTimeZone(oldTimezone).getOffset(now) != TimeZone.getTimeZone(newTimezone).getOffset(now)) {
     prefs.edit().putString(PREF_TIMEZONE, newTimezone).commit();
     Logger.log("TimeZone time change");
    //update alarms
}

I added zone's time check, because very often I found out, that althought zones change, they do not differ in time in any way. Also some users claimed that they didn't even travel anywhere far when numerous changes in zones were detected - just regular trips to work and back.

我增加了区域的时间检查,因为我经常发现,所有的区域都在变化,它们在时间上没有任何不同。还有一些用户声称,当检测到许多区域的变化时,他们甚至没有去任何很远的地方——只是定期往返上班。

Check limited number of unneeded operations.

检查有限数量的不必要的操作。

#2


0  

Avoid listening for:

避免监听:

<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />

it seems they get called regularly also when time has not been set nor timezone changed. I suspect it is linked to the user using "use network provided time zone and time" in the phone's settings.

似乎他们在没有设定时间和时区改变的时候也会被定期的呼叫。我怀疑它与用户在手机设置中使用“使用网络提供的时区和时间”有关。

If you really do need to listen for these broadcasts you should check if time really has changed significantly or if its just a millisecond correction from the network provided time

如果你真的需要收听这些广播,你应该检查时间是否真的发生了显著的变化,或者它是否仅仅是网络提供的时间的一毫秒的修正