Multiple, configurable widgets - update issue

江枫思渺然 提交于 2019-12-11 23:24:43

问题


When I'm adding single widget I can configure it properly and it's updated as supposed to after initial configuration. But when I add second and next instances of widget with the same or different configuration onUpdate is not triggered. Well, to be specific onUpdate is executed just before Configuration is set, when Configuration Activity is on top, so it works like:

1 widget - OK

2 widgets - First OK, Second not updated

3 widgets - First OK, Second OK, Third not updated

and so on.

Behavior tested on Android 4.3. I've no idea why update is not working as supposed to.

"OK" Button in Configuration Activity (straight from: http://developer.android.com/guide/topics/appwidgets/index.html#Configuring):

 public void ok(View v){

     Intent intent = getIntent();
     Bundle extras = intent.getExtras();
     if (extras != null) {
         mAppWidgetId = extras.getInt(
                 AppWidgetManager.EXTRA_APPWIDGET_ID, 
                 AppWidgetManager.INVALID_APPWIDGET_ID);
     }

     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
     RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_lay);
     appWidgetManager.updateAppWidget(mAppWidgetId, views);

     Log.e("mAppWidgetId", "id: "+mAppWidgetId);

     Intent resultValue = new Intent();  
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);  
     setResult(RESULT_OK, resultValue);  
     saveSettings();
     finish();  
 }

OnUpdate part in Widget Provider:

ComponentName thisWidget = new ComponentName(context, MyWidget.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
            int myConfig = settings.getInt("myConfig"+widgetId, -1);
            if(myConfig != -1){
                Log.d("widget id", widgetId+" ");
                updateWidget(db, widgetId, myConfig);
            }

回答1:


This answer solved the issue: https://stackoverflow.com/a/5363553/1449961:

The problem was on NOT SENDING ACTION_APPWIDGET_UPDATE so before closing preferences I send broadcast:

Intent updateIntent = new Intent(this, CallBackWidget.class);
updateIntent.setAction("PreferencesUpdated");
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
sendBroadcast(updateIntent);

and in onReceive method of widget I check for broadcast:

if ("PreferencesUpdated".equals(action)) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
    appWidgetManager.updateAppWidget(appWidgetId, views);
    int[] appWidgetIds = new int[] {appWidgetId};
    onUpdate(context, appWidgetManager, appWidgetIds);
}  

but I feel like it's a hack rather than a good solution, so better way is still welcome.



来源:https://stackoverflow.com/questions/20776449/multiple-configurable-widgets-update-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!