Update AppWidget Periodically from a service
<p>This is what I want from my AppWidget:</p>

<ol>
<li>configuration activity comes up, when widget is added to the screen // good so far</li>
<li>after configuration is saved, a service is started that updates the widget // good so far</li>
<li>schedule an alarm periodically to run the service that updates the widget. // having troubles here</li>
</ol>

<p>This is seriously giving me grey hair already, and I don't know what to do anymore. How do you set the update rate for an AppWidget from a service? I can update the widget from the service, but when I try to set the alarm, it does not get to the <code>onReceive()</code> method on the AppWidget.</p>

<p>Here is the code for the service update:</p>

<pre><code>Intent updateWidget = new Intent();
updateWidget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateWidget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{appWidgetId});
Uri data = Uri.withAppendedPath(Uri.parse(WeatWidgetProvider.URI_SCHEME +
 "://widget/id/"), String.valueOf(appWidgetId));
updateWidget.setData(data);
PendingIntent updatePend = PendingIntent.getBroadcast(this, 0, updateWidget, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+ updateRate, updateRate, updatePend);
</code></pre>

<p>And in the <code>onreceive()</code> of WidgetProviderClass:</p>

<pre><code>public void onReceive(Context context, Intent intent){
  super.onReceive(context, intent);
  Log.d("OnReceive", "OnReceive called");
  String action = intent.getAction();
  Log.d("Action", "OnReceive:Action: " + action);
  if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
    int appwidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    if(appwidgetId != AppWidgetManager.INVALID_APPWIDGET_ID){
      this.onDeleted(context, new int[] {appwidgetId});
    }
  } else if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)){
    if(!URI_SCHEME.equals(intent.getScheme())){
      final int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
      for(int appWidgetId:appWidgetIds){
        SharedPreferences prefs = context.getSharedPreferences(WeatForecastConfigure.WEATHER_PREF_NAME, Context.MODE_PRIVATE);
        update = prefs.getInt(WeatForecastConfigure.REFRESH_UPDATE, -1);
        System.out.println(update);
        if(update != -1){
          Intent widgetUpdate = new Intent();
          widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
          widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });
          // make this pending intent unique by adding a scheme to it
          widgetUpdate.setData(Uri.withAppendedPath(Uri.parse(WeatWidgetProvider.URI_SCHEME +
           "://widget/id/"), String.valueOf(appWidgetId)));
          PendingIntent newPending = PendingIntent.getBroadcast(context.getApplicationContext(), 0, widgetUpdate, PendingIntent.FLAG_UPDATE_CURRENT);
          // schedule the updating
          AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          alarms.setRepeating(AlarmManager.ELAPSED_REALTIME,
           SystemClock.elapsedRealtime(), WeatForecastConfigure.convertToMillis(update), newPending);
        }
      }
    }
    super.onReceive(context, intent);
  }else{
    super.onReceive(context, intent);
  }
}
</code></pre>

<p>For <code>onUpdate()</code> commented lines are deliberate.</p>

<pre><code>public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
  Log.d("OnUpdate", "OnUpdate called");
  for (int appWidgetId : appWidgetIds) {
    // context.startService(new Intent(context,WeatService.class));
  }
  //super.onUpdate(context, appWidgetManager, appWidgetIds);
}
</code></pre>

<p>Please help. I have no idea how to set the alarm to update. Whether I do it from the configuration class or from a service, it does not work. Thanks.</p>
