AppWidgets在自定义启动器中不起作用

时间:2012-09-28 22:36:26

标签: android android-widget android-appwidget

我在编写启动器时遇到appwidgets的问题。只有Clock小部件正常工作,另一个不能: Android widgets problem

代码的某些部分:

final AppWidgetHostView hostView = mAppWidgetHost.createView(this, appWidgetId, providerInfo);

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

            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
        } else {
            // Otherwise just add it
            completeAddAppWidget(appWidgetId, info.container, info.screen, providerInfo);

            // Exit spring loaded mode if necessary after adding the widget
            exitSpringLoadedDragModeDelayed(true, false);
        }

completeAddAppWidget方法:

private void completeAddAppWidget(final int appWidgetId, long container,
                                      int screen, AppWidgetProviderInfo providerInfo) {
        // Calculate the grid spans needed to fit this widget
        CellLayout layout = getCellLayout(container, screen);

        int[] spanXY = getSpanForWidget(providerInfo, null);

        // Try finding open space on Launcher screen
        // We have saved the position to which the widget was dragged-- this
        // really only matters
        // if we are placing widgets on a "spring-loaded" screen
        int[] cellXY = mTmpAddItemCellCoordinates;
        int[] touchXY = mPendingAddInfo.dropPos;
        boolean foundCellSpan = false;
        if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
            cellXY[0] = mPendingAddInfo.cellX;
            cellXY[1] = mPendingAddInfo.cellY;
            foundCellSpan = true;
        } else if (touchXY != null) {
            // when dragging and dropping, just find the closest free spot
            int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1],
                    spanXY[0], spanXY[1], cellXY);
            foundCellSpan = (result != null);
        } else {
            foundCellSpan = layout
                    .findCellForSpan(cellXY, spanXY[0], spanXY[1]);
        }

        if (!foundCellSpan) {
            if (appWidgetId != -1) {
                // Deleting an app widget ID is a void call but writes to disk
                // before returning
                // to the caller...
                new Thread("deleteAppWidgetId") {
                    public void run() {
                        mAppWidgetHost.deleteAppWidgetId(appWidgetId);
                    }
                }.start();
            }
            showOutOfSpaceMessage();
            return;
        }

        // Build Launcher-specific widget info and save to database
        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(
                appWidgetId);
        launcherInfo.spanX = spanXY[0];
        launcherInfo.spanY = spanXY[1];

        LauncherModel.addItemToDatabase(this, launcherInfo, container, screen,
                cellXY[0], cellXY[1], false);

        if (!mRestoring) {
            // Perform actual inflation because we're live
            launcherInfo.hostView = mAppWidgetHost.createView(this,
                    appWidgetId, providerInfo);

            launcherInfo.hostView.setAppWidget(appWidgetId, providerInfo);
            launcherInfo.hostView.setTag(launcherInfo);

            mWorkspace.addInScreen(launcherInfo.hostView, container, screen,
                    cellXY[0], cellXY[1], launcherInfo.spanX,
                    launcherInfo.spanY, isWorkspaceLocked());

            addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, providerInfo);
        }
    }

1 个答案:

答案 0 :(得分:1)

我有一段时间没遇到这个问题。 你必须在每个appwidget更改后调用startListening(),添加删除等。