当 Android 小部件 RemoteViewService 被销毁时
When Android widget RemoteViewService gets destroyed
我正在研究 Widget
,其中包含 ListView
作为集合。我已经创建了 RemoteViewService
的具体实现并实现了 onGetViewFactory()
方法以提供 RemoteViewsService.RemoteViewsFactory
的具体实现。
我使用以下代码将服务设置为 RemoteView
:
Intent intent= new Intent();
intent.setAction("remote_service");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.widget_list, intent);
我在RemoteViewFactory
的具体实现中保存List对象。
查询步骤:
- 在主屏幕上放置了小部件。
- 这会导致创建 RemoteViewFactory。
- 将设备置于睡眠模式。
- 现在 RemoteViewService 会销毁导致 RemoteViewFactory
垃圾收集的具体实施,因此
List<String>
对象销毁。
请推荐?
一个RemoteViewsService
,就像一个AppWidgetProvider
,不会永远活下去。您的 RemoteViewsService
,在其 onGetViewFactory()
方法中,应该 return 您的 RemoteViewsFactory
的新实例。 RemoteViewsFactory
所需的任何数据都需要持久化(例如,SQLiteDatabase
)并根据需要检索。
我正在研究 Widget
,其中包含 ListView
作为集合。我已经创建了 RemoteViewService
的具体实现并实现了 onGetViewFactory()
方法以提供 RemoteViewsService.RemoteViewsFactory
的具体实现。
我使用以下代码将服务设置为 RemoteView
:
Intent intent= new Intent();
intent.setAction("remote_service");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.widget_list, intent);
我在RemoteViewFactory
的具体实现中保存List对象。
查询步骤:
- 在主屏幕上放置了小部件。
- 这会导致创建 RemoteViewFactory。
- 将设备置于睡眠模式。
- 现在 RemoteViewService 会销毁导致 RemoteViewFactory
垃圾收集的具体实施,因此
List<String>
对象销毁。
请推荐?
一个RemoteViewsService
,就像一个AppWidgetProvider
,不会永远活下去。您的 RemoteViewsService
,在其 onGetViewFactory()
方法中,应该 return 您的 RemoteViewsFactory
的新实例。 RemoteViewsFactory
所需的任何数据都需要持久化(例如,SQLiteDatabase
)并根据需要检索。