How to catch widget size changes on devices where onAppWidgetOptionsChanged not getting called?

本小妞迷上赌 提交于 2019-12-18 02:54:50

问题


Devices like Samsung Galaxy S3 working with Android version 4.1.2 has a bug which prevents onAppWidgetOptionsChanged to be called.

So, how can we get information related to changed sizes?


回答1:


I am adding to Frankish's answer with how I handle receiving this broadcast:

@Override
public void onReceive(Context context, Intent intent) {
    // Handle TouchWiz
    if(intent.getAction().contentEquals("com.sec.android.widgetapp.APPWIDGET_RESIZE")) {
        handleTouchWiz(context, intent);
    }
    super.onReceive(context, intent);
}

private void handleTouchWiz(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    int appWidgetId = intent.getIntExtra("widgetId", 0);
    int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
    int widgetSpanY = intent.getIntExtra("widgetspany", 0);

    if(appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
        Bundle newOptions = new Bundle();
        // We have to convert these numbers for future use
        newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74);
        newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74);

        onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }
}

You may need to change how you handle that bundle if you are using MAX_HEIGHT or MAX_WIDTH which I am not.




回答2:


I have discovered that you can catch RESIZE action in onReceive function.

if(intent.getAction().contentEquals("com.sec.android.widgetapp.APPWIDGET_RESIZE"))

You'll get the following values from this intent's getExtras() bundle:

int appWidgetId = bundle.getInt("widgetId"); // bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);


int widgetSpanX = bundle.getInt("widgetspanx", 4);
int widgetSpanY = bundle.getInt("widgetspany", 1);

Now you can use these values to update the view or store them in a static integer map variable to use in the next onUpdate.



来源:https://stackoverflow.com/questions/17396045/how-to-catch-widget-size-changes-on-devices-where-onappwidgetoptionschanged-not

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