How to add a widget to the code on the main screen

I am trying to write a simple launcher application. In this, I am trying to add a default widget, for example, android clock. I download all the information about the default widgets, such as x, y positions, width and height, and packaging information. After downloading this, I get the appwidgetId for the home screen. The code is this.

public void bindAppWidgets(Launcher.DesktopBinder binder,
                LinkedList<WidgetInfo> appWidgets) {
            if (!appWidgets.isEmpty()) {
                for(int k=0;k<appWidgets.size();k++){
                    final WidgetInfo item = appWidgets.get(k);
                    System.out.println("Item to be added in the view :::::::::"+item.toString());
                    int appWidgetId = item.appWidgetId;
                    if(item.itemType == LauncherProvider.Widgets.ITEM_TYPE_DEFAULT_APPWIDGET){
                        bindDefaultAppwidgets(item);
                        continue;
                        item.appWidgetId=mAppWidgetHost.allocateAppWidgetId();
                        LauncherModel.updateItemInDatabase(getApplicationContext(), item);
                        appWidgetId=item.appWidgetId;
                    }

                    final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
                    System.out.println("the Appwidget info is*******"+appWidgetInfo);
                    item.hostView =(LauncherAppWidgetHostView) mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);

                    if (LOGD) {
                        d(TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId,
                                appWidgetInfo));
                    }

                    item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
                    item.hostView.setTag(item);
                    MyAbsoluteLayout.LayoutParams lp=new MyAbsoluteLayout.LayoutParams(item.spanX, item.spanY, item.cellX, item.cellY);
                    System.out.println("after load the hostview ::::::::::: :::::::::"+item.toString());
                    mDragLayer.addView(item.hostView,lp);
                }
                mDragLayer.requestLayout();
            }

        }

Now I get a null value for widgets in AppWidgetProviderInfo. Because of this, I could not load hostview in the homescreen. Can someone suggest me add a widget to the home screen with the appwidgetpicker app.

+3
source share
1 answer

, ( ).

, , "bindAppWidgetId". : AppWidgets AppWidgetHost - Android .

, . , . , , , .

+1

All Articles