从onReceive方法而不是onUpdate中更改Android小部件上的TextView
我正在开发一个应用程序,它运行一个定期发送有关其当前状态的广播的服务。这些广播由my从onReceive方法而不是onUpdate中更改Android小部件上的TextView,android,android-widget,android-service,android-broadcast,Android,Android Widget,Android Service,Android Broadcast,我正在开发一个应用程序,它运行一个定期发送有关其当前状态的广播的服务。这些广播由myAppWidgetProvider使用其onReceive方法接收: @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (intent.getAction().equals(ACTION_STATUS_CHANGED)) { B
AppWidgetProvider使用其onReceive
方法接收:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_STATUS_CHANGED)) {
Bundle b = intent.getBundleExtra("updateInfo");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_app_control);
// Doesn't work for some reason
views.setTextViewText(R.id.informationTextView, b.getString(MyService.BIT_OF_INFORMATION));
// Update widgets
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, AppControlWidgetProvider.class));
this.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
但是,即使我添加了对onUpdate
的调用,使用setTextViewText
设置的新文本也不会进入实际的小部件。如果我将从中获取信息的捆绑包提供给类的整个实例(为了测试,将其保存在属性中),并从onUpdate
方法中对setTextViewText
进行完全相同的调用,TextView
将按预期更新
到目前为止,我已经在一定程度上浏览了这个主题,但是在其他任何地方,从onReceive
方法中设置TextView
文本的方式与我的方式完全相同(除非我遗漏了什么),似乎都很好。亲爱的考拉,请编辑添加。。用于发送更新的代码段。您好,relsell,您指的是发送广播的代码吗?或者你实际上是指将更新发送到小部件的部分?从某种意义上说,后者发生在实际代码的最后三行(在//更新widgets
之后)。如果您指的是前者,我想再次指出,广播确实会发送到AppWidgetProvider
。