10

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.

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.

Here's my BroadcastReceiver's onReceive code

@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?

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

4

2 回答 2

9

我仍然不知道为什么时区更改和时间设置如此频繁地被调用,但我能够找到一个解决方案,让我找出何时真正需要做出反应。

另外,我现在只听时区更改。

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
}

我添加了区域的时间检查,因为我经常发现,尽管区域发生了变化,但它们在时间上并没有任何不同。此外,一些用户声称,当检测到区域的大量变化时,他们甚至没有去任何地方——只是定期上班和回来。

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

于 2014-11-13T09:53:47.267 回答
0

避免听:

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

当时间尚未设置或时区未更改时,它们似乎也会定期被调用。我怀疑它与手机设置中使用“使用网络提供的时区和时间”的用户相关联。

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

于 2014-11-03T06:15:37.463 回答