I have a Android App widget and a button on the widget. I have set the update time period to 30mins but I also want to update the widget whenever I touch the button. here's my code:
RemoteViews remoteV = new RemoteViews(context.getPackageName(), R.layout.widgetmenu);
Intent intentSync = new Intent(context, MessMenuWidgetProvider.class);
PendingIntent pendingSync = PendingIntent.getBroadcast(context,0, intentSync,0);
remoteV.setOnClickPendingIntent(R.id.imageButtonSync,pendingSync);
appWidgetManager.updateAppWidget(awID, remoteV);
I have set the update time to 30 mins so in every 30mins the function onUpdate() is called. What I want to achieve is to call onUpdate() manually using the button. But it's not happening. Any help?
This is extremely easy. Below is the modified code to make the "onUpdate" method of your widget called each time the button is clicked.
RemoteViews remoteV = new RemoteViews(context.getPackageName(), R.layout.widgetmenu);
Intent intentSync = new Intent(context, MessMenuWidgetProvider.class);
intentSync.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); //You need to specify the action for the intent. Right now that intent is doing nothing for there is no action to be broadcasted.
PendingIntent pendingSync = PendingIntent.getBroadcast(context,0, intentSync, PendingIntent.FLAG_UPDATE_CURRENT); //You need to specify a proper flag for the intent. Or else the intent will become deleted.
remoteV.setOnClickPendingIntent(R.id.imageButtonSync,pendingSync);
appWidgetManager.updateAppWidget(awID, remoteV);
Now each time you click that button, the broadcast AppWidgetManager.ACTION_APPWIDGET_UPDATE will be sent to your widget and the method you have inside that class will handle the update. So either the onUpdate method is called, or the onReceive method is called. Whichever you have specified.
RemoteViews remoteV = new RemoteViews(context.getPackageName(), R.layout.widgetmenu);
Intent intentSync = new Intent(context, MessMenuWidgetProvider.class);
intentSync.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
PendingIntent pendingSync = PendingIntent.getBroadcast(context,0, intentSync, PendingIntent.FLAG_UPDATE_CURRENT);
remoteV.setOnClickPendingIntent(R.id.imageButtonSync,pendingSync);
appWidgetManager.updateAppWidget(awID, remoteV);
It will call onReceive(); In your onReceive() method just add the following to call onUpdate manually.
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
Bundle extras = intent.getExtras();
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), MessMenuWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
onUpdate(context, appWidgetManager, appWidgetIds);
}
}
If MessMenuWidgetProvider is your AppWidgetProvider subclass, your PendingIntent will trigger onReceive() on it, not onUpdate(). You would need to call setAction() on the Intent, supplying ACTION_APPWIDGET_UPDATE, plus fill in the EXTRA_APPWIDGET_IDS extra with your app widget's ID in a one-argument int[]. In other words, you need to set up the Intent to be the same as the Intent structure that AppWidgetManager would use to trigger onUpdate() in your AppWidgetProvider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With