android appwidget 不会从 Activity 更新

发布于 2024-10-06 23:41:02 字数 323 浏览 5 评论 0原文

我有一个简单的应用程序小部件,我想在活动(在同一个应用程序中)中发生操作时更新它。在 onUpdate() 中,我立即更新小部件,效果很好。在我的活动中,我在 appwidget 中调用与 onUpdate() 中调用的相同静态更新方法来更新视图。小部件未更新。

我可以将代码直接跟踪到 AppWidgetManager.updateAppWidget() 方法中,这一切都很好,但小部件没有更新。

我能看到的唯一可能的区别是,当从活动的上下文与应用程序小部件的 onUpdate() 方法的上下文调用它时,传递到静态更新方法的上下文对象是不同的。然而,网上有很多这方面的例子,所以我希望它应该有效。

I have a simple appwidget and I want to update it when an action occurs in an activity (in the same app). in onUpdate(), I immediately update the widget, which works fine. In my activity, I call the same static update method in my appwidget that is called in onUpdate() to update the views. the widget is not updated.

I can trace the code right into the AppWidgetManager.updateAppWidget() method, and all this good, but the widget does not update.

The only possible difference I can see is that the context object passed into my static update method is different, when it's called from the context of an activity vs. the context of a appwidget's onUpdate() method. however, there are lots of examples on the web of this so I expect it should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-10-13 23:41:02

如果没有看到您的代码,我不能 100% 确定您要如何执行此操作,但是这是我使用的方法。在我的 Activity 中,我有以下方法:

private void updateAllWidgets(){
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MyWidget.class));
    if (appWidgetIds.length > 0) {
        new MyWidget().onUpdate(this, appWidgetManager, appWidgetIds);
    }
}

其中 MyWidget 是 appwidget 的类。我可以从 Activity 中的任何位置调用此方法来更新我的所有应用程序小部件。

Without seeing your code I'm not 100% sure how you are trying to do it, however here is the method I use. Within my Activity I have the following method:

private void updateAllWidgets(){
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MyWidget.class));
    if (appWidgetIds.length > 0) {
        new MyWidget().onUpdate(this, appWidgetManager, appWidgetIds);
    }
}

Where MyWidget is the class of the appwidget. I can call this method from anywhere within my Activity to update all my appwidgets.

杯别 2024-10-13 23:41:02

与其使用静态方法,不如利用小部件已经是广播接收器的事实,并在清单中为其注册更新意图。然后,每当您需要从活动更新它时

//in your activity
sendBroadcast(new Intent(MyWidget.ACTION_UPDATE).putExtra(whatever));  



//In widget class
public static final String ACTION_UPDATE = "com.example.UPDATE_MY_WIDGET";

,只需调用清单文件的接收器标签内的

<intent-filter>
  <action android:name="com.example.UPDATE_MY_WIDGET"/>
</intent-filter>

Rather than use a static method take advantage of the fact that the widget is already a broadcast receiver and register an update intent for it in your manifest. Then whenever you need to update it from the activity just call

//in your activity
sendBroadcast(new Intent(MyWidget.ACTION_UPDATE).putExtra(whatever));  



//In widget class
public static final String ACTION_UPDATE = "com.example.UPDATE_MY_WIDGET";

and inside the receiver tags of your manifest file

<intent-filter>
  <action android:name="com.example.UPDATE_MY_WIDGET"/>
</intent-filter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文