• <tfoot id='Iu1sG'></tfoot>
      <bdo id='Iu1sG'></bdo><ul id='Iu1sG'></ul>
    <legend id='Iu1sG'><style id='Iu1sG'><dir id='Iu1sG'><q id='Iu1sG'></q></dir></style></legend>

      <small id='Iu1sG'></small><noframes id='Iu1sG'>

      <i id='Iu1sG'><tr id='Iu1sG'><dt id='Iu1sG'><q id='Iu1sG'><span id='Iu1sG'><b id='Iu1sG'><form id='Iu1sG'><ins id='Iu1sG'></ins><ul id='Iu1sG'></ul><sub id='Iu1sG'></sub></form><legend id='Iu1sG'></legend><bdo id='Iu1sG'><pre id='Iu1sG'><center id='Iu1sG'></center></pre></bdo></b><th id='Iu1sG'></th></span></q></dt></tr></i><div id='Iu1sG'><tfoot id='Iu1sG'></tfoot><dl id='Iu1sG'><fieldset id='Iu1sG'></fieldset></dl></div>

      1. 如何在 android 应用程序中创建小部件?(使用 App Widget Host 类?)

        时间:2023-09-08
          <tbody id='gPSZt'></tbody>
        <i id='gPSZt'><tr id='gPSZt'><dt id='gPSZt'><q id='gPSZt'><span id='gPSZt'><b id='gPSZt'><form id='gPSZt'><ins id='gPSZt'></ins><ul id='gPSZt'></ul><sub id='gPSZt'></sub></form><legend id='gPSZt'></legend><bdo id='gPSZt'><pre id='gPSZt'><center id='gPSZt'></center></pre></bdo></b><th id='gPSZt'></th></span></q></dt></tr></i><div id='gPSZt'><tfoot id='gPSZt'></tfoot><dl id='gPSZt'><fieldset id='gPSZt'></fieldset></dl></div>
        • <legend id='gPSZt'><style id='gPSZt'><dir id='gPSZt'><q id='gPSZt'></q></dir></style></legend>

            <bdo id='gPSZt'></bdo><ul id='gPSZt'></ul>

                • <small id='gPSZt'></small><noframes id='gPSZt'>

                • <tfoot id='gPSZt'></tfoot>

                  本文介绍了如何在 android 应用程序中创建小部件?(使用 App Widget Host 类?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要创建一个包含多个小部件的应用程序.这些不是桌面小部件.我需要能够与这些小部件进行交互,就好像它们是桌面小部件一样,但它们需要封装在更大的应用程序中.(每个小部件在单击时都有自己的功能和行为.)

                  I need to create an application that contains multiple widgets. These are not desktop widgets. I need to be able to interact with these widgets as if they were desktop widgets, but they need to be encased inside a larger application. (Each widget has it's own functionality and behavior when clicked.)

                  这在android中可能吗?或者我是否需要创建一个应用程序并创建每个我想像小部件一样实际作为视图的对象?

                  Is this possible in android? Or do I need to create an application and create each object that I'd like to behave like a widget actually as a view?

                  例如.父应用程序适用于汽车.应用内"小部件的示例包括:换油历史(显示最近三个换油日期的列表,单击日期将打开收据扫描等)、胎压监测器、圈速历史(显示最近四圈, 捏和展开会显示多于四个)等.

                  Ex. The parent app is for a car. Example of "in app" widgets are: oil change history (list of last three oil change dates visible, clicking on a date will open a scan of the receipt, etc.), tire pressure monitor, lap speed history (shows last four laps, pinching and expanding will show more than four), etc.

                  我可以将这些对象中的每一个都制作成小部件吗?还是必须是应用内的视图?

                  Can I make each of these objects widgets? Or do they have to be views inside the app?

                  Android 开发者的 App Widget Host 页面 提到:AppWidgetHost 为希望在其 UI 中嵌入应用小部件的应用(例如主屏幕)提供与 AppWidget 服务的交互."
                  有没有人创建了自己的 App Widget Host 或直接使用这个类?

                  The Android developer's App Widget Host page mentions: "The AppWidgetHost provides the interaction with the AppWidget service for apps, like the home screen, that want to embed app widgets in their UI."
                  Has anyone created their own App Widget Host or worked directly with this class?

                  推荐答案

                  如果你想在你的应用程序中嵌入一个特定的小部件,并且知道包名和类名:

                  If you want to embed a specific widget in your app, and know the package name and class name:

                  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;
                      }
                  }
                  

                  这篇关于如何在 android 应用程序中创建小部件?(使用 App Widget Host 类?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在小部件单击时将任务置于最前面 下一篇:使用自定义字体显示时间问题

                  相关文章

                  <legend id='kctjs'><style id='kctjs'><dir id='kctjs'><q id='kctjs'></q></dir></style></legend>
                  <i id='kctjs'><tr id='kctjs'><dt id='kctjs'><q id='kctjs'><span id='kctjs'><b id='kctjs'><form id='kctjs'><ins id='kctjs'></ins><ul id='kctjs'></ul><sub id='kctjs'></sub></form><legend id='kctjs'></legend><bdo id='kctjs'><pre id='kctjs'><center id='kctjs'></center></pre></bdo></b><th id='kctjs'></th></span></q></dt></tr></i><div id='kctjs'><tfoot id='kctjs'></tfoot><dl id='kctjs'><fieldset id='kctjs'></fieldset></dl></div>

                  <small id='kctjs'></small><noframes id='kctjs'>

                    <bdo id='kctjs'></bdo><ul id='kctjs'></ul>

                    1. <tfoot id='kctjs'></tfoot>