ilyes90
ilyes90

Reputation: 117

Android 2.2 updating widget never works for me

Hello I am new to widget programming and this makes me really crazy !! I have been trying to solve this for days with no hope, I found examples- a lot of them- I test, and never works. I tested in both the simulator and real phone.

The script is simple, the same I've found everywhere, even here in stackoverflow, I have a little script in onUpdate

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

 // Create some random data
            int number = (new Random().nextInt(100));

     Log.v("Provider", "called");

     // Get the layout for the App Widget and attach an on-click listener
    // to the button
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);

    views.setTextViewText(R.id.textViewwid, String.valueOf(number));

        // Create an Intent to launch ExampleActivity
    Intent intent = new Intent(context, HelloAndroidActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.textViewwid, pendingIntent);



    // Tell the AppWidgetManager to perform an update on the current app widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

then somewhere in my activity, i want to update the widget

    Log.v("Response", "my activity lunched");
   // Find widget id from launching intent
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    // If they gave us an intent without the widget id, just bail.
    if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
        Log.v("Response", "no appwidget id provided");
        finish();
    }

    //configureWidget(getApplicationContext());
    Log.v("Response", "appwidget id is provided!!");

    // Make sure we pass back the original appWidgetId before closing the activity
    Intent resultValue = new Intent(this, WidgetProvider.class);
    //resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);


    resultValue.setAction("android.appwidget.action.APPWIDGET_UPDATE");
 int[] ids = {mAppWidgetId};
 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
 sendBroadcast(resultValue);

So the code should be working this way: when I click the widget, the Activity launches, then the activity call the onUpdate function to update the widget, but the widget never updates.

The strange is that log.v("provider","called" is shown everytime I click the widget, so I am very sure the onupdate is called, but settextviewtext never works, except the first time when the widget is created, any ideas??

Upvotes: 2

Views: 213

Answers (1)

canova
canova

Reputation: 3975

I was having the same problem of updating widgets in activity. Check out dave.c's answer in the topic below. His updateAllWidgets function is a life-saver answer in this issue.

android appwidget won't update from activity

Upvotes: 2

Related Questions