Bazouk55555
Bazouk55555

Reputation: 567

Launch a widget from an application

In y application, using this code I can get the list of the providers for all the widgets installed on my device:

    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    List <ComponentName> cn = new ArrayList<>();
    List<AppWidgetProviderInfo> infoList = manager.getInstalledProviders();
    for (AppWidgetProviderInfo info : infoList) {
            cn.add(info.provider);
    }

If I want to launch one of these widget, is there a way to do it? (I think I might need the appWidgetId but I dont know how to retrieve it).

Upvotes: 1

Views: 258

Answers (1)

V&#225;clav Hodek
V&#225;clav Hodek

Reputation: 668

I copy this from my very old code, so it may need some polishing. It's not going to work out of the box, but it contains all the important steps to show you how to do it.

  1. First, you need to show a picker to let the user select the widget:
    private void createWidget() {
        try {
            awhId = // generate your unique ID
            awh = new AppWidgetHost(ctx, awhId);
            int appWidgetId = awh.allocateAppWidgetId();
            Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
            pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            pickIntent = widgetAddEmptyData(pickIntent);
            startActivityForResult(pickIntent, ActivityMain.REQUEST_CODE_WIDGET_ADD);
        } catch (Exception e) {
            widgetError(e.getMessage(), e);
        }
    }


   public Intent widgetAddEmptyData(Intent pickIntent) {
        ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
        pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
        ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
        pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
        return pickIntent;
    }
  1. You have to correctly process the response of the picker.
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        boolean processed = false;
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == ActivityMain.REQUEST_CODE_WIDGET_ADD) {
                widgetConfigure(data);
                processed = true;
            }
            if (requestCode == ActivityMain.REQUEST_CODE_WIDGET_CONFIGURED) {
                widgetSave(data);
                processed = true;
            }
        } else if (resultCode == Activity.RESULT_CANCELED && data != null) {
            int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            if (appWidgetId != -1) {
                try {
                    if (awh == null) {
                        awh = new AppWidgetHost(ctx, awhId);
                    }
                    awh.deleteAppWidgetId(appWidgetId);
                } catch (Exception e) {}
            }
        }
        if (!processed) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void widgetConfigure(Intent data) {
        try {
            Bundle extras = data.getExtras();
            int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            AppWidgetProviderInfo appWidgetInfo = wm.getAppWidgetInfo(appWidgetId);
            if (appWidgetInfo.configure != null) {
                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
                intent.setComponent(appWidgetInfo.configure);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                startActivityForResult(intent, ActivityMain.REQUEST_CODE_WIDGET_CONFIGURED);
            } else {
                widgetSave(data);
            }
        } catch (Exception e) {
            widgetError(e.getMessage(), e);
        }
    }

    private void widgetSave(final Intent data) {
        try {
            Bundle extras = data.getExtras();
            final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            final AppWidgetProviderInfo appWidgetInfo = wm.getAppWidgetInfo(appWidgetId);

            // store information about created widget
            store(MyApps.WIDGET_HOST_ID, awhId);
            store(MyApps.WIDGET_ID, appWidgetId);
            store(MyApps.WIDGET_ICON, appWidgetInfo.icon);
            store(MyApps.WIDGET_PACKAGE, appWidgetInfo.provider.getPackageName());
            store(MyApps.WIDGET_CLASS, appWidgetInfo.provider.getClassName());
    }
  1. Once you have the widget created and you have stored its data, you can host it.
AppWidgetManager wm = AppWidgetManager.getInstance(getContext());
awh = new FaAppWidgetHost(getContext(), myAppItem.getWidgetHostId());
AppWidgetProviderInfo appWidgetInfo = wm.getAppWidgetInfo(myAppItem.getWidgetId());
AppWidgetHostView hostView = awh.createView(getContext(), myAppItem.getWidgetId(), appWidgetInfo);
hostView.setAppWidget(myAppItem.getWidgetId(), appWidgetInfo);
awh.startListening();

Upvotes: 2

Related Questions