I'm having some issues adding widgets to the homescreen in a custom Launcher I've been working on.
I've been able to generate a list of widgets to add with an AppWidgetManager and I've developed the workflow for adding the widget to my home screen. The code is not quite what is below but looks something like the following:
AppWidgetHost widget_host = new AppWidgetHost(this, 1);
AppWidgetManager widget_manager = AppWidgetManager.getInstance(this);
int widget_id = widget_host.allocateAppWidgetId();
AppWidgetProviderInfo widget_provider = ... //from an array;
Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, widget_provider.provider);
startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);
if (widget_provider.configure != null) {
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(widget_provider.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
} else {
createWidget(widget_id);
}
I then have an onActivityResult method that leads to the configuration of widgets if necessary, and the createWidget method uses the createView method of the AppWidgetHost.
This workflow works but the ACTION_APPWIDGET_BIND intent asks the user for permission to bind the app, which is kind of annoying. My understanding is that only system apps can request this permission and that I am out of luck in binding widgets without asking for this permission while the app is running. On the other hand, I know there are many other launchers out there and they can all add widgets seamlessly, so there must be another approach to this.
Any advice would be greatly appreciated!
Cheers