views:

203

answers:

1

Hi,

How can we update the View of a Home Screen Widget on the onReceive method of AppWidgetProvider?.

I am trying to update the TextView of my Home screen widget but it seems that I cant access the TextView of my AppWidgetProvider on onReceive method.

Here is a sample code of my onReceive

public void onReceive(Context context,Intent intent) {
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
        AppWidgetManager.EXTRA_APPWIDGET_ID,
        AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            String msg = "null";
            try {
                msg = intent.getStringExtra("msg");
            } catch (NullPointerException e) {

            }


            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            // code for gathering the text to update the TextView


            }

    }
        super.onReceive(context, intent);
}
+1  A: 

You can't update TextView. You can only build complete new widget layout (RemoteViews) and publish it to home screen.

Fedor
Is there a way wherein if we click at a Button on the Home Screen Widget, it will update the TextView in it?
Unfortunately no. Only send intent to broadcast receiver and rebuild new RemoteViews.
Fedor