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

发布于 2024-11-04 19:07:05 字数 2163 浏览 0 评论 0原文

我正在尝试编写一个简单的启动器应用程序。我正在尝试添加一个默认小部件,例如 android 时钟。我正在加载默认小部件的所有信息,例如 x,y 位置,宽度和高度以及包信息。加载后我我正在获取主屏幕的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();
            }

        }

现在我在 AppWidgetProviderInfo 中获得小部件的空值。因为我无法在主屏幕中加载主机视图。任何人都可以建议我在不使用 appwidgetpicker 应用程序的情况下在主屏幕中添加小部件吗?

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2024-11-11 19:07:06

除非启动器具有特殊权限并且是系统应用程序(将允许其使用此权限),否则无法使用此类功能。

有很多地方都会告诉您同样的信息,只需搜索“bindAppWidgetId”即可。这是一个例子:
将 AppWidget 绑定到 AppWidgetHost - Android
原因是安全。

我认为即使对于您自己的应用程序小部件,您也无法绑定。但是,您始终可以通过创建自定义视图来模仿应用程序小部件。它们比应用程序小部件拥有更多的功能,因此您可以在那里做任何您想做的事情。

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" . here's an example:
Binding AppWidgets to AppWidgetHost - Android
the reason for this is security .

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文