2

I have application with starting activity which shows app widgets that can be add to homescreen.

When user clicks on that app widget, application should send intent to open list of widgets.

But I can't find any intent for opening launcher with widgets list to pick. Is it possible?

pjanecze
  • 3,145
  • 3
  • 21
  • 28

1 Answers1

3
static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
static final String SEARCH_WIDGET = "search_widget";
void pickappWidget(){
     int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();

     Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
     pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     // add the search widget
     ArrayList<AppWidgetProviderInfo> customInfo =
             new ArrayList<AppWidgetProviderInfo>();
     AppWidgetProviderInfo info = new AppWidgetProviderInfo();
     info.provider = new ComponentName(getPackageName(), "XXX.YYY");
     info.label = getString(R.string.group_widgets);
     info.icon = R.drawable.ic_allapps;
     customInfo.add(info);
     pickIntent.putParcelableArrayListExtra(
             AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
     ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
     Bundle b = new Bundle();
     b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
     customExtras.add(b);
     pickIntent.putParcelableArrayListExtra(
             AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
     // start the pick activity
     startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
}

and in your onActivityResult function,process the message retur from widget picker dialog

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mWaitingForResult = false;
    if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
        switch (requestCode) {
            case REQUEST_PICK_APPWIDGET:
                addAppWidget(data);
                break;
        }
}


void addAppWidget(Intent data) {

    int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

    if (appWidget.configure != null) {
        // Launch over to configure widget, if needed
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidget.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
    } else {
        // Otherwise just add it
        onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
    }
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
tomoroho
  • 51
  • 3
  • Once the widget is chosen in the Picker, won't everything else happen on it's own, without processing the result? I mean the Config Activity (specified in meta in manifest) should be called, and then the WidgetProvider when the widget has been added to the launcher? – Andrew Mackenzie Nov 24 '13 at 13:08
  • This looks good. How to you get the reference to AppWidgetHost (mAppWidgetHost) in order to allocate the appWidgetID? – Andrew Mackenzie Nov 24 '13 at 13:13
  • Maybe your answer is about adding Widgets to your own AppWidgetHost? If so, maybe that's why answer was not accepted - as it is about adding to the homescreen, and then we need to get the Launcher's AppWidgetHost....? – Andrew Mackenzie Nov 24 '13 at 13:24