I have made an application
and a widget
. I have to update my widget
from the application i.e. send some data to the widget. I used the sendBroadcast(Intent intent)
method from the application. It is well received in the onReceive(Context context, Intent intent)
method of MyWidgetProvider app. I am able to get the data, But the onUpdate()
method does not seem to work and hence no UI for the widget.
this is the code for widget app
public class MyWidgetProvider extends AppWidgetProvider {
public String team1name;
public String team2name;
public String team1score;
public String team2score;
public static boolean done=false;
public void onReceive(Context c, Intent intent){
Bundle extras = intent.getExtras();
if (extras == null) {
return;
}
team1name = extras.getString("team1name");
team2name = extras.getString("team2name");
team1score = extras.getString("team1score");
team2score = extras.getString("team2score");
done=true;
}
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
Log.e("WidgetExample", "The victory is mine");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
// Set the text
remoteViews.setTextViewText(R.id.update, team1name);
// Register an onClickListener
Intent i = new Intent(context, MyWidgetProvider.class);
i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, i, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}