Widgets don't respond when re-added through code

瘦欲@ 提交于 2019-12-17 07:34:26

问题


I am working on a launcher. It let's users add widgets from the list, it works fine. I store information about the widget (PackageName, ClassName and it's co-ordinates on the screen) in my database when the user adds one to the screen.

When the application is restarted (or the device itself) I add those (user selected) widgets back through code:

// APPWIDGET_HOST_ID is any number you like
appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
AppWidgetProviderInfo newAppWidgetProviderInfo = new AppWidgetProviderInfo();

// Get an id
int appWidgetId = appWidgetHost.allocateAppWidgetId();

// Get the list of installed widgets
List<AppWidgetProviderInfo> appWidgetInfos = new ArrayList<AppWidgetProviderInfo>();
appWidgetInfos = appWidgetManager.getInstalledProviders();

for(int j = 0; j < appWidgetInfos.size(); j++)
{
    if (appWidgetInfos.get(j).provider.getPackageName().equals(PackageName) && appWidgetInfos.get(j).provider.getClassName().equals(ClassName))
    {
        // Get the full info of the required widget
        newAppWidgetProviderInfo = appWidgetInfos.get(j);
        break;
    }
 }

// Create Widget
AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);

// Add it to your layout
RelativeLayout widgetLayout = (RelativeLayout) findViewById(R.id.widgetLayout);
widgetLayout.addView(hostView);

The widget is successfully added to the layout but it seems like it's not enabled/updated. It doesn't respond to clicks (for instance, Analog Clock) and doesn't show any data (for instance, Music player widget), although I have put this:

@Override
protected void onStart() {
    super.onStart();
    appWidgetHost.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    appWidgetHost.stopListening();
}

I am stuck on it since quite a long time. Searched a lot but it seems I'm the only one in this world with this problem.


回答1:


We can invoke a dialog asking user to permit our app to be able to bind widget IDs with the following code:

Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, sSavedWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, mWidgetInfo.provider);
startActivityForResult(intent, REQUEST_BIND_WIDGET);

And bind widget IDs to make them actually work with:

Bundle opts = new Bundle();
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);

widgetManager.bindAppWidgetIdIfAllowed(widgetId, mWidgetInfo, opts);

For more information refer:

  • AppWidgetManager.ACTION_APPWIDGET_BIND
  • AppWidgetManager.bindAppWidgetIdIfAllowed()

I wish someone would have answered the same 5 years ago.



来源:https://stackoverflow.com/questions/11470189/widgets-dont-respond-when-re-added-through-code

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