Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

android - Widgets don't respond when re-added through code

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...