我一直致力于实施可以手动更新的 AppWidget
。但是,出于某种原因,Widget没有更新,除了第一次显示它,并且该更新是通过 Configuration Activity
进行的,它几乎与 {具有相同的代码{1}} 位于 onReceive
类中。
我正在做的总结是,当需要更新时,我发送一个在Manifest中的WidgetProvider中注册的广播,在onReceive中接收到它后,我提取值并更新小部件,但是,窗口小部件不会更新。我尝试直接在onReceive中更新它,尝试手动调用onUpdate,并尝试更新提供程序外的小部件,但没有运气。下面是代码的简化版本,我很确定我遗漏了一些简单的内容,欢迎提出任何建议,如果您需要任何进一步的信息,请提前告知我们。
AppWidgetProvider
,但它没有得到更新,我尝试了我找到的所有解决方案,但没有运气,谢谢。
Note that the widget is appearing fine, the initial layout is shown, and the onReceive method is receiving the broadcast correctly since the logs are shown in the LogCat
onReceive Method:
//onReceive
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("com.example.android.myproject.REQUEST_WIDGET_UPDATE"))
{
//ComponentName, RemoteViews and AppWidgetManager
ComponentName provider = new ComponentName(context.getApplicationContext(), ReceiverAppWidgetProvider.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
//Change the TextView value
String timeLeft = intent.getStringExtra("timeLeft");
views.setTextViewText(R.id.txtv_time_remaining, timeLeft);
//Update the widget
manager.updateAppWidget(provider, views);
//Test calling onUpdate
//onUpdate(context, manager, manager.getAppWidgetIds(provider));
//Log the values for the objects for debugging
MyLog.log(timeLeft);
MyLog.log(intent.getAction());
MyLog.log(provider.toString());
MyLog.log(views.toString());
MyLog.log(manager.toString());
}
else
{
super.onReceive(context, intent);
}
}
Broadcast in Manifest:
<!-- App Widget Provider -->
<receiver
android:name="com.example.android.myproject.widgets.ReceiverAppWidgetProvider"
android:label="@string/widget_title"
android:icon="@drawable/background_splash_screen">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="com.example.android.myproject.REQUEST_WIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_info"/>
</receiver>
Widget XML configuration:
答案 0 :(得分:4)
发现问题,只需重新启动设备使其再次工作,代码本身没有任何问题。如果您使用的是仿真器,也可能会出现此问题,因此重新启动它也可以解决问题。
How can I update the UI for an android appwidget
<强> Reported issue:
强>
https://code.google.com/p/android/issues/detail?id=8889