1 package ${packageName}; 2 3 import android.appwidget.AppWidgetManager; 4 import android.appwidget.AppWidgetProvider; 5 import android.content.Context; 6 import android.widget.RemoteViews; 7 8 /** 9 * Implementation of App Widget functionality. 10 <#if configurable> 11 * App Widget Configuration implemented in {@link ${className}ConfigureActivity ${className}ConfigureActivity} 12 </#if> 13 */ 14 public class ${className} extends AppWidgetProvider { 15 16 @Override 17 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 18 // There may be multiple widgets active, so update all of them 19 final int N = appWidgetIds.length; 20 for (int i=0; i<N; i++) { 21 updateAppWidget(context, appWidgetManager, appWidgetIds[i]); 22 } 23 } 24 25 <#if configurable> 26 @Override 27 public void onDeleted(Context context, int[] appWidgetIds) { 28 // When the user deletes the widget, delete the preference associated with it. 29 final int N = appWidgetIds.length; 30 for (int i=0; i<N; i++) { 31 ${className}ConfigureActivity.deleteTitlePref(context, appWidgetIds[i]); 32 } 33 } 34 </#if> 35 36 @Override 37 public void onEnabled(Context context) { 38 // Enter relevant functionality for when the first widget is created 39 } 40 41 @Override 42 public void onDisabled(Context context) { 43 // Enter relevant functionality for when the last widget is disabled 44 } 45 46 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 47 int appWidgetId) { 48 49 <#if configurable> 50 CharSequence widgetText = ${className}ConfigureActivity.loadTitlePref(context, appWidgetId); 51 <#else> 52 CharSequence widgetText = context.getString(R.string.appwidget_text); 53 </#if> 54 // Construct the RemoteViews object 55 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.${class_name}); 56 views.setTextViewText(R.id.appwidget_text, widgetText); 57 58 // Instruct the widget manager to update the widget 59 appWidgetManager.updateAppWidget(appWidgetId, views); 60 } 61 } 62 63 64