Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating android app widget manually using a button on the widget

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?

like image 676
iitum studant Avatar asked Oct 25 '14 21:10

iitum studant


3 Answers

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.

like image 161
Seth Avatar answered Oct 22 '22 05:10

Seth


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);
       }
}
like image 27
iitum studant Avatar answered Oct 22 '22 03:10

iitum studant


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.

like image 22
CommonsWare Avatar answered Oct 22 '22 04:10

CommonsWare