update AppWidget Periodically from a service
<p>This is what i want from my appwidget:</p>

<blockquote>
  <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>
</blockquote>

<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 already update the widget from the service, but when i try to set the alarm, it does not get to the onReceive() method on the appwidget. 
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 onreceive() 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 onUpdate():commented lines are delibrate</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>
