Appwidgetprovider Public Void Onenabled (context Context) Does Not Effect Widget
Solution 1:
Register for the broadcast in your manifest:
<receiverandroid:name="MyWidget"android:label="Widget"><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE" /><actionandroid:name="android.appwidget.action.APPWIDGET_ENABLED" /></intent-filter>
....
</receiver>
Also note this about onEnabled():
"This is called when an instance the App Widget is created for the first time. For example, if the user adds two instances of your App Widget, this is only called the first time."
You also have the fact that onUpdate() will be called anyway when an instance is added, so doing the same thing in onEnabled() may be redundant.
Also, once you get that AsyncTask to execute, you're going to have a host of problems, first of which is the Context object is not an Activity.
EDIT: Now that your method is being called, you will need to set the text using RemoteViews. You don't need to do it in onPostExecute() since this is cross-process update.
protected Void doInBackground(String... arg0) {
StringbeerText= readJSONFeed(arg0[0]);
AppWidgetManagermgr= AppWidgetManager.getInstance(c);
int[] appWidgetIds = mgr.getAppWidgetIds(newComponentName(c, HelloWidget.class));
//You need a static method somewhere (I usually put in widget provider class) //that builds the RemoteView and sets everything up.RemoteViewsrv= HelloWidget.buildRemoteViews(c, mgr, appWidgetIds);
rv.setTextViewText(R.id.text_view_in_layout, beerText);
mgr.updateAppWidget(appWidgetIds, rv);
}
That will update all your AppWidget's, if you only want one to update, you will need to pass in it's id.
Solution 2:
The buildRemoteViews() method isn't how you get a RemoteViews object in a widget; use a constructor instead.
RemoteViewsviews=newRemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
No offense, but this is pretty clearly laid in the example in the API guide for App Widgets.
Post a Comment for "Appwidgetprovider Public Void Onenabled (context Context) Does Not Effect Widget"