0

I've read that using System.exit(0) is frowned upon when it comes to Java and Android, but so far I can find no alternative for what I'm trying to accomplish. To be clear, this is for a Watchface, which is simply a service extending CanvasWatchFaceService. I cannot call finish() in this case. I've also tried stopService and startService with no success.

The issue I'm trying to solve: It's well known that changing timezones on your device will not be reflected on a watchface unless it is restarted. In my testing, I found that System.currentTimeMillis() quite literally does not respond to timezone changes in Android Wear. The watchface must be restarted in order for it to show the correct time after a timezone change.

So I built a system with the following code:

private final BroadcastReceiver timeChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (!restarting) {
            if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                if (upSeconds >= 15) {
                    System.exit(0);
                } else {
                    restarting = true;
                    int delay = ((15 - upSeconds) * 1000);

                    new CountDownTimer(delay, delay) {
                        @Override
                        public void onTick(long millisUntilFinished) { }
                        @Override
                        public void onFinish() {
                            System.exit(0);
                        }
                    }.start();
                }
            }
        }
    }
};

The delay is in case a user triggers a time zone change more frequently than 15 seconds at a time. Android Wear seems to detect system exits that are too frequent and replace the watchface with the "Simple" watchface.

It seems to work great, Android Wear automatically boots the watchface back up on exit. But I would eventually like to put this app on the Google Play Store, so I thought I should make sure I'm not playing with fire here.

rjr-apps
  • 352
  • 4
  • 13
  • Have you seen: http://stackoverflow.com/questions/11257449/system-exit-in-android? Possible dup. – Jared Burrows Jul 09 '16 at 22:56
  • Thanks for the link! That helps, but it does not fully answer my question. My question is regarding the `CanvasWatchFaceService` in Android Wear specifically. – rjr-apps Jul 09 '16 at 22:58
  • 1
    All of the code on the [watchface training](https://developer.android.com/training/wearables/watch-faces/drawing.html) handles timezone changes just fine. Why do you think you have to exit to get the time to update? – ianhanniballake Jul 09 '16 at 23:01
  • "It's well known that changing timezones on your device will not be reflected on a watchface unless it is restarted" -- citation, please. – CommonsWare Jul 09 '16 at 23:14
  • This is what I was basing that statement off of: https://code.google.com/p/android/issues/detail?id=77074. It has been my experience with all the custom watchfaces I have downloaded as well. However, after seeing ian's comment, I tried one of the standard Google watchfaces and found it does not have the issue. I wonder if the answer lies somewhere in the link he mentioned. – rjr-apps Jul 10 '16 at 00:21

1 Answers1

0

I can't believe I went through all that work when the proper solution was so simple. Thanks ianhanniballake for the link!

After looking at the Analog Watchface Sample, I found that all I needed to do was use mCalendar.setTimeZone(TimeZone.getDefault());. In many places I was directly comparing the time in milliseconds fetched with long now = System.currentTimeMillis();, so I simply did a now = mCalendar.getTimeInMillis() to take care of that.

Now the watchface changes time properly when the timezone is changed. I guess the other watchfaces I downloaded did not properly handle this!

rjr-apps
  • 352
  • 4
  • 13