【问题标题】:Widget not updating when Activity content changes in Android当 Android 中的 Activity 内容更改时,小部件不更新
【发布时间】:2011-12-29 20:55:12
【问题描述】:

我有一个小部件,其内容与实际应用中看到的非常相似,即图像和文本的比例较小。

我正在使用以下代码在活动 UI 更新后更新小部件 - 只是它不工作并且小部件不更新:

Intent i = new Intent(mContext, ExtensionOfAppWidgetProvider.class);
i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
mContext.sendBroadcast(i);

如果不删除小部件然后将其放回主屏幕,我无法调用 AppWidgetProvider 类的 onUpdate()。

感谢任何帮助。

谢谢。

【问题讨论】:

    标签: android android-widget broadcastreceiver


    【解决方案1】:

    这是罪魁祸首……我的 AppWidgetProvider 类的 onReceive() 中缺少几行代码。

    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    
    int[] ids = mgr.getAppWidgetIds(new ComponentName(context, PromoStackWidgetProvider.class));
    
    for(int id : ids) {
            mgr.notifyAppWidgetViewDataChanged(id, R.id.stack_view);
        }
    

    【讨论】:

      【解决方案2】:

      确保您的小部件在您的 AndroidManifest.xml 中将自己作为注册的广播接收器

      【讨论】:

        猜你喜欢
        • 2011-05-03
        • 2013-05-05
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 2021-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多