All app widget instances are managed by a corresponding "widget provider" class:
public class MyWidgetProvider extends AppWidgetProvider
The widget provider class will be triggered by the Android operating system when to refresh all instantiated widgets:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
AlarmInfo alarmInfo = CheckForNextAlarm(context);
RefreshNotificationForLockScreen(context, alarmInfo);
final int numWidgets = appWidgetIds.length;
for (int i=0; i < numWidgets; i++) {
int widgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);
String displayText;
if (alarmInfo.alarmTime.isEmpty())
{
displayText = "\noff";
}
else
{
displayText = alarmInfo.alarmTime + " \n(" + alarmInfo.durationUntilAlarm + ")";
}
remoteViews.setTextViewText(R.id.text, displayText);
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refreshImageBtn, pendingIntent);
appWidgetManager.updateAppWidget(widgetId,remoteViews);
}
}