Is it possible to include an Android App widget in an existing application (AppWidgetHost) without the "Select widgets" list?

We are trying to place a third-party widget inside our own application, and we are trying to figure out how it can be added to our application installation, without having to select it from the "Select widgets" list. The reason is that our client needs users to select this particular widget (and we try to save the code separately so that they can update this particular widget inside our application), and this is not a great UX for the user to choose another randomly. Is there a way to filter the Select Widget list? Or just show him the one we need to show? And since we have access to this third-party widget source, will this help?

So far, we can say that the only way to add a widget to the widget host is to assign an identifier, run ACTION.APPWIDGET_PICK, and the user can select it there. But we really need to find a way for this list to be filtered, or this third-party widget will be installed when our application (host) is also installed.

Any ideas?

Thank!

+3
source share
2 answers

You cannot filter the list, and you cannot install the application without clicking on it. But you can check the results of the user's selection and reject it if they chose the wrong widget ...

0
source

This is how you insert a widget into your application without a list of your favorite widgets. The user will simply see a confirmation dialog.

public boolean createWidget(View view, String packageName, String className) {
    // Get the list of installed widgets
    AppWidgetProviderInfo newAppWidgetProviderInfo = null;
    List<AppWidgetProviderInfo> appWidgetInfos;
    appWidgetInfos = mAppWidgetManager.getInstalledProviders();
    boolean widgetIsFound = false;
    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);
            widgetIsFound = true;
            break;
        }
    }

    if (!widgetIsFound) {
        return false;
    } else {
        // Create Widget
        int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
        AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
        hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);

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

        // And bind widget IDs to make them actually work
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);

            if (!allowed) {
                // Request permission - https://stackoverflow.com/a/44351320/1816603
                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
                final int REQUEST_BIND_WIDGET = 1987;
                startActivityForResult(intent, REQUEST_BIND_WIDGET);
            }
        }

        return true;
    }
}
0

All Articles