2

我有启动活动的应用程序,它显示可以添加到主屏幕的应用程序小部件。

当用户单击该应用程序小部件时,应用程序应发送意图以打开小部件列表。

但我找不到任何打开带有小部件列表的启动器的意图。可能吗?

4

1 回答 1

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);
}

并在您的 onActivityResult 函数中,处理来自小部件选择器对话框的消息返回

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);
    }
}
于 2012-08-11T09:36:11.423 回答