Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TIMEZONE_CHANGED intent being received every few seconds

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

like image 258
Koger Avatar asked Apr 19 '13 21:04

Koger


1 Answers

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.

like image 141
Koger Avatar answered Oct 06 '22 06:10

Koger