【问题标题】:Possible to include an Android App Widget inside an existing app (AppWidgetHost), without the Select Widgets list?可以在现有应用程序 (AppWidgetHost) 中包含 Android 应用程序小部件,而无需选择小部件列表?
【发布时间】:2011-05-15 12:24:14
【问题描述】:

我们正在尝试在我们自己的应用中托管一个第 3 方小部件,并且我们正在尝试弄清楚如何在我们的应用安装时添加它,而无需用户从“选择小部件”列表中选择它。原因是,我们的客户需要他们的用户选择这个特定的小部件(我们试图将代码分开,以便他们可以在我们的应用程序内自行更新这个特定的小部件),这对于一个用户可以偶然选择不同的。有没有办法过滤选择小部件列表?或者只是让它显示我们需要它显示的那个?既然我们可以访问这个第 3 方小部件的源代码,那会有帮助吗?

到目前为止,我们可以说,将小部件添加到小部件主机的唯一方法是分配一个 id,运行 ACTION.APPWIDGET_PICK 并且用户可以从中选择。但我们确实需要找到一种方法以某种方式过滤该列表,或者在安装我们的应用程序(主机)时安装此第三方小部件。

有什么想法吗?

谢谢!

【问题讨论】:

    标签: android installation android-widget


    【解决方案1】:

    您无法过滤列表,并且您无法在没有用户点击的情况下安装该应用程序。但是您可以检查用户选择的结果,如果他们选择了错误的 Widget,则拒绝它...

    【讨论】:

      【解决方案2】:

      这是在没有选择小部件列表的情况下在应用中嵌入小部件的方式。用户只会看到一个确认对话框。

      public boolean createWidget(View view, String packageName, String className) {
          // Get the list of installed widgets
          AppWidgetProviderInfo newAppWidgetProviderInfo = null;
          List<AppWidgetProviderInfo> appWidgetInfos;
          appWidgetInfos = mAppWidgetManager.getInstalledProviders();
          boolean widgetIsFound = false;
          for(int j = 0; j < appWidgetInfos.size(); j++)
          {
              if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className))
              {
                  // Get the full info of the required widget
                  newAppWidgetProviderInfo = appWidgetInfos.get(j);
                  widgetIsFound = true;
                  break;
              }
          }
      
          if (!widgetIsFound) {
              return false;
          } else {
              // Create Widget
              int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
              AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
              hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
      
              // Add it to your layout
              LinearLayout widgetLayout = view.findViewById(R.id.widget_view);
              widgetLayout.addView(hostView);
      
              // And bind widget IDs to make them actually work
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                  boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
      
                  if (!allowed) {
                      // Request permission - https://stackoverflow.com/a/44351320/1816603
                      Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
                      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
                      final int REQUEST_BIND_WIDGET = 1987;
                      startActivityForResult(intent, REQUEST_BIND_WIDGET);
                  }
              }
      
              return true;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-05
        相关资源
        最近更新 更多