2

在编写启动器时,我遇到了 appwidgets 的问题。只有时钟小部件正常工作,其他不: Android小部件问题

部分代码:

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

1 回答 1

1

我前段时间遇到过这个问题。您必须在每次 appwidget 更改、添加删除等后调用 startListening()。

于 2014-12-30T13:50:53.860 回答