AppWidgetManager manager = AppWidgetManager.getInstance(Context); Hope this solve your problem.
On Jan 11, 10:14 am, John Gaby <jg...@gabysoft.com> wrote: > I am trying to create an App Widget for the Home screen which displays > a line of text. When the user clicks on the Widget, I want the text to > change. I have pieced together the following which seems to work, > however I am concerned about how I am providing access to the > AppWidgetManager in the onReceive call. I need the AppWidgetManager > there because I wish to change the text. I tried adding a class > member variable, 'm_appWidgetManager', which I set during the onUpdate > call. However when I get the onReceive call, this value is null. Is > this because the onReceive is called with a different instance of my > class, and if so, why is that? If I chang m_appWidgetManager to be > static, and it all seems to work, but this does not seem like the > correct solution. > > Can anyone tell me how to do the properly? > > Thanks. > > package com.gabysoft.appwidgettest; > > import android.app.PendingIntent; > import android.appwidget.AppWidgetManager; > import android.appwidget.AppWidgetProvider; > import android.content.ComponentName; > import android.content.Context; > import android.content.Intent; > import android.util.Log; > import android.widget.RemoteViews; > > public class HelloWidget extends AppWidgetProvider > { > public static String ACTION_WIDGET_RECEIVER = > "ActionReceiverWidget"; > > static AppWidgetManager m_appWidgetManager; > > @Override > public void onUpdate(Context context, AppWidgetManager > appWidgetManager, int[] appWidgetIds) > { > m_appWidgetManager = appWidgetManager; > > Log.d("GabySoft", "HelloWidget:onUpdate: m_appWidgetManager = > " + m_appWidgetManager); > > RemoteViews remoteViews = new > RemoteViews(context.getPackageName(), R.layout.main); > > Intent active = new Intent(context, HelloWidget.class); > active.setAction(ACTION_WIDGET_RECEIVER); > > PendingIntent pendingIntent = > PendingIntent.getBroadcast(context, 0, active, 0); > > remoteViews.setOnClickPendingIntent(R.id.widget_textview, > pendingIntent); > > appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); > } > > @Override > public void onReceive(Context context, Intent intent) > { > Log.d("GabySoft", "HelloWidget:onReceive: manager = " + > m_appWidgetManager); > > if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) > { > RemoteViews remoteViews = new > RemoteViews(context.getPackageName(), R.layout.main); > ComponentName thisWidget = new ComponentName(context, > HelloWidget.class); > > remoteViews.setTextViewText(R.id.widget_textview, "Some > other text"); > m_appWidgetManager.updateAppWidget(thisWidget, > remoteViews); > } > > super.onReceive(context, intent); > }} > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en