【发布时间】:2012-01-07 10:48:24
【问题描述】:
我有一个成功运行的 appwidget。现在当手机重新启动时, 该小部件会丢失其所有数据并仅位于主屏幕上。因为当手机重新启动时我无法从广播接收器更新小部件,所以我创建了一个通知 这会导致小部件的配置活动。用户再次设置配置并离开配置活动后,没有任何效果;空闲的小部件仍然存在?(用户必须删除小部件并再次创建小部件)..假设我没有正确接收小部件 ID,或者我在广播接收器中做错了,并假设将所有代码移动到小部件方法中的onEnable?..我如何正确刷新小部件。请记住,所有小部件更新都是通过服务完成的。
顺便说一句,我在广播接收器中有此代码用于 boot_completed 操作:
public void onReceive(final Context context, Intent intent) {
String info = context.getResources().getString(R.string.restart_setting);
int[] allids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, MyWidgetProvider.class));
for(int appWidgetId:allids){
NotificationManager mnotifyManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.icon, "Weather Update", System.currentTimeMillis());
notify.defaults = Notification.DEFAULT_SOUND;
notify.defaults = Notification.DEFAULT_VIBRATE;
Intent Settings = new Intent(context, WidgetConfigure.class);
Settings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pending = PendingIntent.getActivity(context, 0, weatherSettings, PendingIntent.FLAG_ONE_SHOT);
notify.setLatestEventInfo(context, "Weather Update", info, pending);
notify.flags = Notification.FLAG_AUTO_CANCEL;
notify.defaults = Notification.DEFAULT_ALL;
mnotifyManager.notify(WEATHER_NOTIFICATION_ID , notify);
}
}
【问题讨论】:
-
我最近尝试将此代码移动到小部件周期中的 onEnabled() 中,但它仍然没有刷新小部件。有可能吗?..
-
如果 appwidgetprovider 本身包含 onReceive 方法,则使用单独的广播
-
你最好使用appwidgetprovider的onReceive来简单的处理id和widget的其他组件