title: How to really update a widget precisely every minute
body: <p>I'm stumped. I know this question has already been answered a hundred times but nothing I've tried works.</p>
  
  <p>My question: I made an Android widget that needs to refresh <strong>precisely</strong> at each minute, much like all clock widgets do. (This widget tells me in how many minutes are left before my train leaves, a one minute error makes it useless).</p>
  
  <p>Here are my attempts to far, and the respective outcomes:</p>
  
  <ul>
  <li>I put <code>android:updatePeriodMillis="60000"</code> in my <code>appwidget_info.xml</code>. However, as specified in <a href="https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html" rel="noreferrer">API Docs</a>, "Updates requested with <code>updatePeriodMillis</code> will not be delivered more than once every 30 minutes" and indeed that's about how often my widget gets updated.</li>
  <li><p>I tried using an <code>AlarmManager</code>. In my <code>WidgetProvider.onEnabled</code>:</p>
  
  <pre><code>AlarmManager am = (AlarmManager)context.getSystemService
          (Context.ALARM_SERVICE);
  Calendar calendar = Calendar.getInstance();
  long now = System.currentTimeMillis();
  // start at the next minute
  calendar.setTimeInMillis(now + 60000 - (now % 60000));
  am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 60000,
          createUpdateIntent(context));
  </code></pre>
  
  <p>however as stated in the <a href="https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29" rel="noreferrer">API docs</a>, "as of API 19, all repeating alarms are inexact" and indeed my widget actually gets updated every five minutes or so.</p></li>
  <li>Based on the previous point I tried setting <code>targetSdkVersion</code> to 18 and saw no difference (updates every five minutes or so).</li>
  <li><p>The <code>setRepeating</code> documentation seems to recommend using <code>setExact</code>. I tried the following. At the end of my update logic:</p>
  
  <pre><code>Calendar calendar = Calendar.getInstance();
  long now = System.currentTimeMillis();
  long delta = 60000 - (now % 60000);
  
  Log.d(LOG_TAG, "Scheduling another update in "+ (delta/1000) +" seconds");
  calendar.setTimeInMillis(now + delta);
  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), //UPDATE_PERIOD_SECONDS * 1000,
          createUpdateIntent(context));
  </code></pre>
  
  <p>It works perfectly for a couple minutes and then reverts to updating every five minutes or so (and not even near minute changes). Here are some timestamps of when the update intent is received:</p>
  
  <ul>
  <li>21:44:17.962</li>
  <li>21:52:37.232</li>
  <li>21:59:13.872</li>
  <li>22:00:00.012 ← hey suddenly it becomes exact again??</li>
  <li>22:01:47.352</li>
  <li>22:02:25.132</li>
  <li>22:06:56.202</li>
  </ul></li>
  <li><p>Some recommend using a <code>Handler</code>. I defined a <code>Service</code> which I start when the widget  provider is enabled, and does this after update code:</p>
  
  <pre><code>int delay = (int)(60000 - (System.currentTimeMillis() % 60000));
  Log.d(LOG_TAG, "Scheduling another update in " + delay/1000 + " seconds");
  new Handler().postDelayed(new Runnable() {
      public void run() {
          Log.d(LOG_TAG, "Scheduled update running");
          updateAppWidget();
      }
  }, delay);
  </code></pre>
  
  <p>and this one works <em>perfectly</em> for several <em>hours</em>, but then the service gets suddenly killed and gets "<a href="https://stackoverflow.com/questions/8044444/android-scheduling-restart-of-crashed-service-with-huge-delay">scheduled to restart after HUGE delay</a>". Concretely, the widget just gets stuck at some point and doesn't get updated at all.</p></li>
  </ul>
  
  <p>Some other options I've seen online: the linked post above suggests creating a foreground service (which, if I understand correctly, means having a permanently visible icon in my already crowded status bar. I don't have one permanent icon for each clock widget I use so that should not be necessary). Another suggestion is to run a high priority thread from the service, which feels awfully overkill.</p>
  
  <p>I've also seen recommendations to use <code>Timers</code> and <code>BroadcastReceiver</code>s but the former is said to be "not appropriate for the task" and I remember having trouble doing the latter. I think I had to do it in a service and then the service gets killed just like when I use <code>Handler</code>s.</p>
  
  <p>It should be noted that the <code>AlarmManager</code> seems to work well when the phone is connected to the computer (presumably because it means the battery is charging), which doesn't help because most of the time I want to know when my train will leave is when I'm already on the way...</p>
  
  <p>As the <code>Handler</code> is perfectly accurate but just stops working after a while, and the <code>AlarmManager</code> option is too inaccurate but does not stop working, I'm thinking of combining them by having <code>AlarmManager</code> start a service every ten minutes or so, and have that service use a <code>Handler</code> to update the display each minute. Somehow I feel this will get detected by Android as a power hog and get killed, and anyway I'm sure I must be missing something obvious. It shouldn't be <em>that</em> hard to do what's essentially a text-only clock widget.</p>
  
  <p>EDIT: if it matters, I'm using my widget on a Samsung Galaxy Note 4 (2016-06-01) with Android 6.0.1.</p>