如何获得appwidget的位置?

时间:2011-03-25 07:29:23

标签: android android-appwidget

我想在屏幕上获取appwidget的位置,但我不知道该怎么做。 我应该使用什么功能? 编辑:我发现在启动器中有这些代码:

/**
 * Add a widget to the workspace.
 *
 * @param data The intent describing the appWidgetId.
 * @param cellInfo The position on screen where to create the widget.
 */
private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
        boolean insertAtFirst) {

    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);

    if (LOGD) d(LOG_TAG, "dumping extras content="+extras.toString());

    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

    // Calculate the grid spans needed to fit this widget
    CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
    int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);

    // Try finding open space on Launcher screen
    final int[] xy = mCellCoordinates;
    if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
        if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        return;
    }

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

    LauncherModel.addItemToDatabase(this, launcherInfo,
            LauncherSettings.Favorites.CONTAINER_DESKTOP,
            mWorkspace.getCurrentScreen(), xy[0], xy[1], false);

    if (!mRestoring) {
        sModel.addDesktopAppWidget(launcherInfo);

        // Perform actual inflation because we're live
        launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);

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

        mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
                launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
    } else if (sModel.isDesktopLoaded()) {
        sModel.addDesktopAppWidget(launcherInfo);
    }
}

它是Build Launcher特定的小部件信息并保存到数据库,我发现数据库是LauncherModel,在LauncherModel中有一个私有的ArrayList,如果我得到了ArrayList,我想我可以得到appwidget的位置,可以有人告诉我如何得到这个。谢谢你。

1 个答案:

答案 0 :(得分:4)