如何托管小部件以及如何始终可靠地更新它们?
How to host widgets and how to update them always reliably?
我在我的应用程序中托管小部件。它们可以从所有已安装小部件的列表中挑选出来并添加到视图中。我将他们的 ID 保存到数据库中,并在重新启动应用程序后再次从这些保存的 ID 中恢复它们。
这在大多数情况下都有效,但并非总是如此。小部件并不总是正确更新。肯定会发生这种情况的一种情况是:
"rebooting phone and starting app before boot-process of device has finished."
(如果我等到启动完成,就可以了!!!)
肯定还有另一种情况,但我无法重现。我重新启动我的应用程序,但它们没有更新。所以我想知道我是否遗漏了一些重要的东西。
这个教程基本用过:
http://leonardofischer.com/hosting-android-widgets-my-appwidgethost-tutorial/#comment-14678
我的代码:
onCreate:
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAWH_AppWidgetHost = new MyAppWidgetHost(MyApp.getContext(), R.string.APPWIDGET_HOST_ID);
onStart:
mAWH_AppWidgetHost.startListening();
onStop:
mAWH_AppWidgetHost.stopListening();
从 id 恢复(保存在数据库中):
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
MyAppWidgetHostView hostView = (MyAppWidgetHostView) mAWH_AppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
mLL_innerLayout.addView((MyAppWidgetHostView) hostView);
我在网络上找不到任何明确说明这一点的网站。查看不同启动器应用程序的源代码也没有让我更清楚。
我遇到了同样的问题,但后来我搬家了
mAWH_AppWidgetHost.startListening();
在 onCreate 方法中和
mAWH_AppWidgetHost.stopListening();
在 onDestroy 方法中。这是建议here
我在我的应用程序中托管小部件。它们可以从所有已安装小部件的列表中挑选出来并添加到视图中。我将他们的 ID 保存到数据库中,并在重新启动应用程序后再次从这些保存的 ID 中恢复它们。
这在大多数情况下都有效,但并非总是如此。小部件并不总是正确更新。肯定会发生这种情况的一种情况是:
"rebooting phone and starting app before boot-process of device has finished." (如果我等到启动完成,就可以了!!!)
肯定还有另一种情况,但我无法重现。我重新启动我的应用程序,但它们没有更新。所以我想知道我是否遗漏了一些重要的东西。
这个教程基本用过:
http://leonardofischer.com/hosting-android-widgets-my-appwidgethost-tutorial/#comment-14678
我的代码:
onCreate:
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAWH_AppWidgetHost = new MyAppWidgetHost(MyApp.getContext(), R.string.APPWIDGET_HOST_ID);
onStart:
mAWH_AppWidgetHost.startListening();
onStop:
mAWH_AppWidgetHost.stopListening();
从 id 恢复(保存在数据库中):
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
MyAppWidgetHostView hostView = (MyAppWidgetHostView) mAWH_AppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
mLL_innerLayout.addView((MyAppWidgetHostView) hostView);
我在网络上找不到任何明确说明这一点的网站。查看不同启动器应用程序的源代码也没有让我更清楚。
我遇到了同样的问题,但后来我搬家了
mAWH_AppWidgetHost.startListening();
在 onCreate 方法中和
mAWH_AppWidgetHost.stopListening();
在 onDestroy 方法中。这是建议here