【问题标题】:Android broadcast receiver not firing up for updating AppWidgetAndroid 广播接收器未启动以更新 AppWidget
【发布时间】:2012-06-21 00:26:33
【问题描述】:

我正在尝试测试我的应用小部件并已在清单中注册接收器,如下所示:

    <receiver android:name=".MyUtilityWidget" android:label="@string/widget_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config" />
    </receiver>

MyUtility 代码:

     public class MyUtilityWidget extends AppWidgetProvider {

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

           my Main Code here.... (widget stuff)
   }

我的小部件启动并执行一次就可以了(因此我将代码留在了这里)。但是广播接收器不工作,我没有收到任何广播。事实上,LogCat 显示意图从未被执行。因此,我的代码块仅在 apk 部署时执行一次,并且不再执行(小部件文本显示,但从不按照我的设置每 30m 更新一次)。

在这里配置...

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget_message"
/>

还有 updatePeriodMillis 不能少于 30 分钟吗?这是调试时非常烦人的问题,因为每次运行我必须等待大约 30 分钟才能检查意图是否触发!

任何帮助表示赞赏。

【问题讨论】:

  • 尝试指定小部件的完整路径而不是“.MyUtilityWidget”

标签: android android-intent android-widget


【解决方案1】:

您需要通过 RemoteViews 抓住您的小部件并相应地更新它。它不只是自动更新。

试试这样的:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
                .getApplicationContext());
        ComponentName thisWidget = new ComponentName(getApplicationContext(),
                MyWidgetProvider.class);
        int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
        final int N =allWidgetIds2.length;
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = allWidgetIds2[i];
            RemoteViews widgetView = new RemoteViews(getPackageName(), R.layout.widget_layout);
            widgetView.setTextViewText(R.id.some_text_view, "some text");

【讨论】:

  • 嗨,对不起,我上面的代码确实有效,所以,也许这只是我的某个地方的错误,可能在用于测试的设备上有一个旧版本(一个旧 Gingerbread 升级 2.3. 7 宏达 G2)。感谢 RemoteViews,我已经在使用它来执行省略的小部件代码片段中的更新,并且更新工作正常。真的,没什么好说的了 - 谢谢,作为一个真正没有答案的问题的唯一答案,我标记你的最好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多