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

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

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

      1. 从 Activity 更新 Android 小部件

        Update Android Widget From Activity(从 Activity 更新 Android 小部件)
          <tbody id='cfhNO'></tbody>

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

                  <legend id='cfhNO'><style id='cfhNO'><dir id='cfhNO'><q id='cfhNO'></q></dir></style></legend>
                • <small id='cfhNO'></small><noframes id='cfhNO'>

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

                  问题描述

                  我有一个小部件,它的设置是当我点击它时,它会在活动中打开一些设置.

                  I have a widget, its setup so that when I click on it, it opens some settings in an activity.

                  views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
                  

                  这会为应用程序配置一些设置.我想要实现的是让小部件更新其视图以在我启动的 Activity 关闭时反映更改的设置.使用更新间隔或任何其他类型的轮询不适合此.

                  This configures some settings for the application. What I want to achieve is to have the widget update its view to reflect the changed settings when the Activity I launch closes. Using the update interval or any other type of polling isn't appropriate for this.

                  我在这里和在 android 文档中看到了这个代码使用的几个地方:

                  I've seen a couple places here and in the android docs this code used:

                  appWidgetManager.updateAppWidget(mAppWidgetId, views);
                  

                  但我不知道如何获取 mAppWidgetId 值.我尝试按照此处的小部件配置活动示例 http://developer.android.com/guide/topics/appwidgets/index.html,但是在下面的代码中,

                  But I don't know how to get the mAppWidgetId value. I tried following the example for a widget configuration activity here http://developer.android.com/guide/topics/appwidgets/index.html, but in the following code,

                      Intent intent = getIntent();
                  Bundle extras = intent.getExtras();
                  if (extras != null) {
                      mAppWidgetId = extras.getInt(
                              AppWidgetManager.EXTRA_APPWIDGET_ID, 
                              AppWidgetManager.INVALID_APPWIDGET_ID);
                  }
                  

                  extras 总是为空,所以我从来没有得到 AppWidgetID.

                  extras is always null, so I never get the AppWidgetID.

                  好的,现在我只是漫无目的.你觉得我能做什么?

                  Ok, now I'm just rambling. What do you think I can do?

                  推荐答案

                  我终于找到了我想要的答案,它在 updateAppWidget 函数的重载中.

                  I finally found the answer I was looking for, it was in an overload of the updateAppWidget function.

                     appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
                  

                  这让我无需知道 appWidgetID 即可访问小部件.我在活动中的最终代码是:

                  This let me access the widget without having to know the appWidgetID. My final code in my activity is then:

                          // Create an Intent to launch ExampleActivity
                      Intent intent = new Intent(this, Settings.class);
                      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                  
                      views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
                  
                      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
                      appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
                      finish();
                  

                  我必须在 Widget 的 onUpdate 方法中进行所有相同的设置工作,但现在每次我退出我的活动时,Widget 都会显示正确的状态.

                  I have to do all the same setup stuff I had to do in the onUpdate method of the Widget, but now every time I exit my activity the Widget is displaying the correct state.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  script.sh: line 1: ./gradlew: No such file or directory on Jenkins(script.sh:第 1 行:./gradlew:Jenkins 上没有这样的文件或目录)
                  Arduino: using Serial and Software Serial with bluetooth module(Arduino:使用带有蓝牙模块的串口和软件串口)
                  Access native serial port on Android(在Android上访问本机串口)
                  Android: USB Communication Android lt;-gt; External Device(Android:USB通信Androidlt;-gt;外接设备)
                  Sending commands from a PC to an Android phone - via physical connection (USB, Serial, etc)(从 PC 向 Android 手机发送命令 - 通过物理连接(USB、串行等))
                  Android: RS232 Serial Communication with android device(Android:与安卓设备的RS232串行通信)
                    • <tfoot id='7uhP4'></tfoot>

                      1. <small id='7uhP4'></small><noframes id='7uhP4'>

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

                        <legend id='7uhP4'><style id='7uhP4'><dir id='7uhP4'><q id='7uhP4'></q></dir></style></legend>

                            <bdo id='7uhP4'></bdo><ul id='7uhP4'></ul>