Manually updating an Android application widget using a button in widgets

I have an Android App widget and a button on widgets. I set the update time period to 30 minutes, but I also want to update the widget when I touch the button. here is 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 set the update time to 30 minutes, so onUpdate () is called every 30 minutes. What I want to achieve is to call onUpdate () manually with a button. But this does not happen. Any help?

+4
source share
4 answers

It is very simple. The following is a modified code so that every time the button is clicked, the onUpdate method of your widget is called.

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);

, , AppWidgetManager.ACTION_APPWIDGET_UPDATE ​​ , , , . onUpdate, onReceive. .

+4
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);

onReceive(); onReceive() , onUpdate .

@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);
       }
}
+5

MessMenuWidgetProvider AppWidgetProvider, PendingIntent onReceive() , onUpdate(). setAction() Intent, ACTION_APPWIDGET_UPDATE, EXTRA_APPWIDGET_IDS extra int[]. , Intent , Intent, AppWidgetManager onUpdate() AppWidgetProvider.

+2

onUpdate() :

intentSync.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);

" " , :

int awID = AppWidgetManager.INVALID_APPWIDGET_ID;

 // Find the widget id from the intent. 
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            awID = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,  AppWidgetManager.INVALID_APPWIDGET_ID);
        }

    // If we got intent without the widget id, exit.
    if (awID == AppWidgetManager.INVALID_APPWIDGET_ID) {
        finish();
    }

To call the button's listener function, use the following code to return the original widget identifier:

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, resultValue); 
0
source

All Articles