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

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

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

      <legend id='ncWLn'><style id='ncWLn'><dir id='ncWLn'><q id='ncWLn'></q></dir></style></legend>

          <bdo id='ncWLn'></bdo><ul id='ncWLn'></ul>
      1. 如何动态更新小部件(不等待 30 分钟调用 onUpdate)?

        时间:2023-09-09

            <tbody id='FV5fH'></tbody>
          <legend id='FV5fH'><style id='FV5fH'><dir id='FV5fH'><q id='FV5fH'></q></dir></style></legend>

              <bdo id='FV5fH'></bdo><ul id='FV5fH'></ul>
              • <small id='FV5fH'></small><noframes id='FV5fH'>

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

                  本文介绍了如何动态更新小部件(不等待 30 分钟调用 onUpdate)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我目前正在学习 Android 中的小部件.

                  I am currently learning about widgets in Android.

                  我想创建一个显示 SSID、RSSI(信号)级别的 WIFI 小部件.

                  I want to create a WIFI widget that will display the SSID, the RSSI (Signal) level.

                  但我也希望能够从我正在运行的计算通过 wifi 的声音质量的服务向它发送数据.

                  But I also want to be able to send it data from a service I am running that calculates the Quality of Sound over wifi.

                  这是我在阅读和快速教程后得到的:

                  Here is what I have after some reading and a quick tutorial:

                  public class WlanWidget extends AppWidgetProvider{
                  
                  RemoteViews remoteViews;
                  AppWidgetManager appWidgetManager;
                  ComponentName thisWidget;
                  WifiManager wifiManager;
                  
                  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                          int[] appWidgetIds) {
                          Timer timer = new Timer();
                          timer.scheduleAtFixedRate(new WlanTimer(context, appWidgetManager), 1, 10000);
                  
                  }
                  
                  
                  private class WlanTimer extends TimerTask{
                  
                          RemoteViews remoteViews;
                          AppWidgetManager appWidgetManager;
                          ComponentName thisWidget;
                  
                  
                  public WlanTimer(Context context, AppWidgetManager appWidgetManager) {
                  
                          this.appWidgetManager = appWidgetManager;
                          remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
                          thisWidget = new ComponentName(context, WlanWidget.class);
                          wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
                  
                  
                  }
                  
                  @Override
                  public void run() {
                  
                          remoteViews.setTextViewText(R.id.widget_textview,
                          wifiManager.getConnectionInfo().getSSID());
                          appWidgetManager.updateAppWidget(thisWidget, remoteViews);
                  }
                  
                  }
                  

                  <小时>

                  上面似乎工作正常,它每 10 秒更新一次小部件上的 SSID.


                  The above seems to work ok, it updates the SSID on the widget every 10 seconds.

                  但是,从我的服务中获取信息的最有效方法是什么?该服务将在我的小部件上定期更新已运行的信息?

                  However what is the most efficent way to get the information from my service that will be already running to update periodically on my widget?

                  还有比使用计时器和计时器任务更好的方法来更新小部件吗?(避免轮询)

                  Also is there a better approach to updating the the widget rather than using a timer and timertask? (Avoid polling)

                  更新

                  根据 Karan 的建议,我在我的服务中添加了以下代码:

                  As per Karan's suggestion I have added the following code in my Service:

                  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
                  ComponentName thisWidget = new ComponentName( context, WlanWidget.class );
                  remoteViews.setTextViewText(R.id.widget_QCLevel, " " + qcPercentage);
                  AppWidgetManager.getInstance( context ).updateAppWidget( thisWidget, remoteViews );
                  

                  <小时>

                  这会在每次 RSSI 级别更改时运行,但它仍然不会更新我的小部件上的 TextView,有什么想法吗?


                  This gets run everytime the RSSI level changes but it still never updates the TextView on my widget, any ideas why?

                  编辑

                  搞定了,谢谢卡兰

                  推荐答案

                  您可以使用以下代码更新widget的数据:

                  You can use the following code to update the data of widget :

                  ComponentName thisWidget = new ComponentName( getContext(), <ProviderClass> );
                  AppWidgetManager.getInstance( getContext() ).updateAppWidget( thisWidget, rempoteViews );

                  这篇关于如何动态更新小部件(不等待 30 分钟调用 onUpdate)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何为 1x1 Android 小部件创建清晰的背景图像? 下一篇:Android - 将 AppWidgets 添加到 Activity

                  相关文章

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

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