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

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

      <legend id='D56OS'><style id='D56OS'><dir id='D56OS'><q id='D56OS'></q></dir></style></legend>
    1. <small id='D56OS'></small><noframes id='D56OS'>

      <tfoot id='D56OS'></tfoot>
      1. Android 小部件未更新 - 小部件 ID 不正确

        时间:2023-09-09
          <tbody id='Qe6hw'></tbody>

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

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

            1. <legend id='Qe6hw'><style id='Qe6hw'><dir id='Qe6hw'><q id='Qe6hw'></q></dir></style></legend>

                <bdo id='Qe6hw'></bdo><ul id='Qe6hw'></ul>
                • 本文介绍了Android 小部件未更新 - 小部件 ID 不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在开发一个小部件并取得了一些进展(我希望如此),但仍然无法让它做我想做的事情.

                  I've been working on a widget and making some advances (I hope) but still can't get it to do what I want.

                  我有一个可以正常工作的配置器活动.当它关闭时,我调用 WordWidget 类中的 updateAppWidget 方法,小部件会更新随机数.

                  I have a configurator activity which works ok. When it closes I call the updateAppWidget method in the WordWidget class and the widget updates the random number.

                  由于 onClick,我无法让随机数发生变化.根据日志,我确实输入了 onReceive() 方法,但仅此而已.

                  What I can't do it get the random number to change because of onClick. According to the log I do enter the onReceive() method, but thats about it.

                  在日志中,我正在打印小部件 ID.我注意到,当从配置器活动运行时,它打印 appWidgetId=33,但是当我按下小部件时,它打印 appWidgetId=24.

                  In the log I'm printing the widget ID. I noticed that when run from the configurator activity it's printing appWidgetId=33, but when I press the widget it prints appWidgetId=24.

                  这是代码:

                  public class WordWidget extends AppWidgetProvider
                  {
                  
                      @Override
                      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
                      {
                          //This is run when a new widget is added or when the update period expires.
                          Log.v("wotd",  "In onUpdate(), updating " + appWidgetIds.length + " widgets");
                          Log.v("wotd",  "   the array is " + appWidgetIds.toString());
                  
                          for(int x = 0; x < appWidgetIds.length; x++)
                          {
                              Integer appWidgetId = appWidgetIds[x];
                  
                  
                              //Method that updates the random value
                              updateAppWidget(context, appWidgetManager, appWidgetId);
                          }
                      }
                  
                      @Override
                      public void onReceive(Context context, Intent intent)
                      {
                          super.onReceive(context, intent);
                          Log.v("wotd", "In onReceive() with intent=" + intent.toString());
                          if (intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE"))
                          {
                              Integer mAppWidgetId;
                              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                  
                              Bundle extras = intent.getExtras();
                              if(extras != null)
                              {
                                  Log.v("wotd", "In onReceive(), extras is valid");
                                  mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
                  
                                  //If they gave us an intent without a valid widget Id, just bail.
                                  if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
                                  {
                                      Log.v("wotd", "In onReceive(), mAppWidgetId is ok");
                                      updateAppWidget(context, appWidgetManager, mAppWidgetId);
                                  }
                              } else
                              {
                                  Log.v("wotd", "In onReceive(), extras is INVALID");
                                  mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
                              }
                          }
                  
                  
                      }
                  
                  
                      static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, Integer appWidgetId)
                      {
                          Log.v("wotd", "In updateAppWidget() with appWidgetId=" + appWidgetId);
                  
                          //Setup onClick
                          Intent widgetIntent = new Intent(context, WordWidget.class);
                          widgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
                          widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                          PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);
                  
                          Integer nextNumb = new Random().nextInt(100);
                          Log.v("wotd", "In updateAppWidget(), nextNumb = " + nextNumb.toString());
                  
                          RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
                          remoteView.setTextViewText(R.id.mainText, String.valueOf(nextNumb));
                          remoteView.setOnClickPendingIntent(R.id.mainText, widgetPendingIntent);
                  
                          // Tell the widget manager
                          appWidgetManager.updateAppWidget(appWidgetId, remoteView);
                      }
                  }
                  

                  这是我触摸小部件时的日志文件(请注意,prefs 活动也有一个日志打印输出,并将小部件 ID 显示为与此不同的数字):

                  This is the log file, from when I touch the widget (note that the prefs activity also has a log printout and shows the widget ID as a different number that this):

                  05-14 17:15:47.453: V/wotd(1585): In onReceive() with intent=Intent { act=android.appwidget.action.APPWIDGET_UPDATE flg=0x10000000 cmp=com.rfxlabs.wordofthedaywidget/.WordWidget bnds=[202,177][222,201] (has extras) }
                  05-14 17:15:47.453: V/wotd(1585): In onReceive(), extras is valid
                  05-14 17:15:47.463: V/wotd(1585): In onReceive(), mAppWidgetId is ok
                  05-14 17:15:47.463: V/wotd(1585): In updateAppWidget() with appWidgetId=24
                  

                  05-14 17:15:47.463: V/wotd(1585): 在 updateAppWidget() 中,nextNumb = 42

                  05-14 17:15:47.463: V/wotd(1585): In updateAppWidget(), nextNumb = 42

                  如你所见,我实际上是在输入我的 updateAppWidget 方法,但由于某种原因 appWidgetId 不正确.

                  So as you can see, I'm actually entering my updateAppWidget method, but for some reason the appWidgetId is incorrect.

                  任何想法为什么会发生这种情况?非常感谢!

                  Any ideas why this is happening? Thanks a lot!

                  推荐答案

                  在你的函数 updateAppWidget 转到这一行:

                  In your function updateAppWidget go to this line:

                  PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);
                  

                  ...并将其更改为:

                  PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, widgetIntent, 0);
                  

                  这篇关于Android 小部件未更新 - 小部件 ID 不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:该类型的方法 startActivity(Intent) 未定义? 下一篇:如何在 iOS 14 Home Widget 中显示当前时间(实时)

                  相关文章

                    <tfoot id='yCW8e'></tfoot>

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

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