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

      <legend id='8zS8K'><style id='8zS8K'><dir id='8zS8K'><q id='8zS8K'></q></dir></style></legend>

        <small id='8zS8K'></small><noframes id='8zS8K'>

          <bdo id='8zS8K'></bdo><ul id='8zS8K'></ul>
      1. <tfoot id='8zS8K'></tfoot>

        Android:如何在创建时更新小部件文本

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

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

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

                  本文介绍了Android:如何在创建时更新小部件文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我看到了几个关于如何更新小部件的问题,但没有什么能帮助我解决我的问题.

                  I have seen several questions regarding how to update widgets but nothing is helping me with my issue.

                  我创建了一个具有文本视图的小部件,我希望在创建小部件时动态更新它.我有一个在屏幕上添加小部件时调用的配置活动.我将值存储在 sharedpreferences 中并在更新时检索它.问题是小部件没有得到更新.知道怎么做吗?我不想在单击小部件后更新文本视图,只需在创建时显示正确的文本.

                  I created a widget that has a textview that I want to dynamically update upon widget creation. I have a configuration activity that is called when adding the widget on the screen. I store the value in the sharedpreferences and retreive it onUpdate. The problem is that the widget doesn't get updated. Any idea how it can be done? I do not want to update the textview after a click on the widget, just get the correct text to be displayed on creation.

                  public class AndroidWidget extends AppWidgetProvider {
                  
                  @Override
                  
                  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                          int[] appWidgetIds) {
                  
                      ComponentName thisWidget = new ComponentName(context,
                              AndroidWidget.class);
                  
                      int[] allWidgetInstancesIds = appWidgetManager
                              .getAppWidgetIds(thisWidget);
                      for (int widgetId : allWidgetInstancesIds) {
                          RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                                  R.layout.widget_layout);
                  
                          // Create an intent that when received will launch the PopUpActivity
                          Intent intent = new Intent(context, AndroidWidget.class);
                          intent.setAction(SHOW_POPUP_DIALOG_ACTION);
                          intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
                  
                          PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                                  widgetId, intent, 0);
                  
                          // Set up the onClickListener of the widget
                  
                          remoteViews.setOnClickPendingIntent(R.id.myText, pendingIntent);
                  
                          SharedPreferences prefs = context.getSharedPreferences(
                                  String.valueOf(widgetId), Context.MODE_PRIVATE);
                  
                          remoteViews.setTextViewText(R.id.myText,
                                  prefs.getString("storedtext", null));
                  
                          appWidgetManager.updateAppWidget(widgetId, remoteViews);
                  
                      }
                  
                      super.onUpdate(context, appWidgetManager, appWidgetIds);
                  
                  }
                  

                  这实际上会更新文本视图,但仅在单击或创建另一个小部件之后.

                  This actually updates the textview but only after clicking or creating another widget.

                  推荐答案

                  @Override
                      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
                  
                          //set widget id
                          ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
                          int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
                  
                          for (int i = 0; i < appWidgetIds.length; i++) {
                              Intent intentService = new Intent(context, WidgetService.class);
                  
                              intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                              intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));
                  
                              RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
                  
                              Intent clickIntent = new Intent(context, MainActivity.class);
                              PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                  
                              widget.setTextViewText(R.id.currency, " Dollar");
                  
                              appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
                          }
                          super.onUpdate(context, appWidgetManager, appWidgetIds);
                  
                      }
                  

                  此代码对我有用.你可以试试这个.

                  This code works for me. You may try this.

                  这篇关于Android:如何在创建时更新小部件文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 iOS 14 Home Widget 中显示当前时间(实时) 下一篇:在 android home 小部件中获取 TextView 的文本(不在活动中)?

                  相关文章

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

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

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