Reputation: 2284
This problem has been discussed before but the solutions seem to not apply to my problem.
I have created a Android Widget. I am working with android 2.0.
The Widget is supposed to update a horizontal progress bar when a certain Intent is received and almost everything works fine. The Broadcast reaches the widget, the updateGUI() method is called all three logging updates can be seen in DDMS.
The problem is, that the progressbar is not updated.
If I use appWidgetManager.updateAppWidget(thisWidget, updateViews); only the Error Widget "Problems lodaing the widget" is shown but without this line I do not know how to update.
The entire Widget looks like this, starting the service and receiviung updates works fine.
package org.ambientdynamix.applications.stepviz3;
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.ProgressBar;
import android.widget.RemoteViews;
public class StepWidget extends AppWidgetProvider {
// Classname for logging purposes
private final String TAG = "StepWidget";
private final String APPWIDGET_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE";
StepCounterService mService;
boolean mBound = false;
Context context;
int[] ids;
int x=0;
@Override
public void onUpdate(Context thecontext, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Log.i(TAG, "onUpdate()");
context=thecontext;
ids=appWidgetIds;
//Start Service
Intent intent = new Intent(context, StepCounterService.class);
context.startService(intent);
super.onUpdate(thecontext, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context thecontext, Intent intent)
{
context=thecontext;
if (intent.getAction().equals(APPWIDGET_UPDATE))
{
int p = intent.getExtras().getInt("steps");
x=p;
Log.i(TAG, "WIDGET: YEAH! TOAST!"+x);
updateGUI();
}
super.onReceive(thecontext, intent);
}
private void updateGUI()
{
if(context!=null)
{
Log.i(TAG, "WIDGET: Update "+x);
RemoteViews updateViews = null;
updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
updateViews.setProgressBar(R.id.progressBarX, 100, x, false);
Log.i(TAG, "WIDGET: Update2 "+x);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//appWidgetManager.updateAppWidget(ids, updateViews);
ComponentName thisWidget = new ComponentName(context, StepWidget.class);
//appWidgetManager.updateAppWidget(thisWidget, updateViews);
Log.i(TAG, "WIDGET: Update3 "+x);
}
}
}
Hack solution based on the Tip by jawsware
OK, got a hack solution:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, StepWidget.class);
ids=appWidgetManager.getAppWidgetIds(thisWidget);
for(int i=0; i<ids.length; i++)
{
appWidgetManager.updateAppWidget(ids[i], updateViews);
}
The problemw as, that the array with the ids was null, thus it could not find the view. This will however upodate all Widgets of this type.
Upvotes: 1
Views: 3834
Reputation: 924
What about trying this?
Add this to onReceive
widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
Change your update to this:
appWidgetManager.updateAppWidget(widgetId, updateViews);
Upvotes: 1