简体   繁体   English

如何将代码中的小部件添加到主屏幕

[英]How to add an widget in code to the home screen

I am trying to write a simple launcher application.In that I am trying to add a default widget like android clock.I am loading all the information of default widgets like x,y positions,width and height and package informations.After loading this I am getting the appwidgetId for the homescreen.The code is like this.我正在尝试编写一个简单的启动器应用程序。我正在尝试添加一个默认小部件,如 android 时钟。我正在加载默认小部件的所有信息,如 x、y 位置、宽度和高度以及 package 信息。加载后我我正在获取主屏幕的 appwidgetId。代码是这样的。

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 am getting null value for the widgets in AppWidgetProviderInfo.because of that I couldn't load the hostview in homescreen.Can any one suggest me to add the widget in home screen with out appwidgetpicker app.现在我得到了 AppWidgetProviderInfo 中小部件的 null 值。因为我无法在主屏幕中加载主机视图。任何人都可以建议我在没有 appwidgetpicker 应用程序的情况下在主屏幕中添加小部件。

such a feature cannot be used unless the launcher has a special permission and is a system app (which will let it to use this permission).除非启动器具有特殊权限并且是系统应用程序(允许它使用此权限),否则无法使用此类功能。

there are plenty of places that will tell you the same, by simply searching for "bindAppWidgetId".通过简单地搜索“bindAppWidgetId”,有很多地方会告诉您相同的信息。 here's an example: Binding AppWidgets to AppWidgetHost - Android the reason for this is security.这是一个示例: Binding AppWidgets to AppWidgetHost - Android这样做的原因是安全性。

i think that even for your own app widgets, you cannot bind.我认为即使对于您自己的应用程序小部件,您也无法绑定。 however, you can always mimic the app-widgets by creating custom views instead.但是,您始终可以通过创建自定义视图来模仿应用程序小部件。 they have much more power than appwidgets, so you can do anything you wish there.它们比 appwidgets 更强大,所以你可以在那里做任何你想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM