非常抱歉我的第一篇帖子没有表述清楚。
这是情况:我有一些需要从互联网上刷新的数据。我们称之为Model
。
我的需求:基本上听起来像是一个MVC模型,其中Model
也被保留在本地(私有)存储中。 Model
及其相关方法是应用程序级别的。 有几个Activity
显示和操作它的不同方面:
- 用户浏览显示
Model
的不同Activity
。当前我为所有元素设置了一个ListActivity
,以及一个用于一个元素详情的Activity
- 有时需要刷新
Model
。当然,这是在不同线程上完成的。 刷新可以从几个Activity
触发。 - 有几个(耗时的)常见任务可以从不同的
Activity
触发 - 我的应用程序在启动和停止时将
Model
加载和保存到私有存储中
我的问题:我不确定在哪里放置Model
和相关任务。此外,我不知道要使用什么机制通知Activity
。目前我想到了两种方法:
- 使用
Service
并发送广播。将保存到磁盘的操作放在Service#onDestroyed()
中,因此我希望将其绑定到Activity
上以最小化它。 在这一点上,我也不确定如何传递更新的信息:是在Binder
中提供一个getter,还是将其包含在广播消息中。 - 自定义
Application
对象,以便全局可用刷新方法和getter。然后我使用AsyncTask
从Activity
中执行更新。 如果有其他Activity
落后于当前Activity
,则当用户返回时,它们将在onResume()
中更新。
我没有使用具有静态方法的类的原因:
- 我需要将
Model
保存和存储到磁盘中。 - 其中一些方法需要Context用于显示toast、通知、缓存等。
此外,我不会将这些功能放在Activity
中,因为有几个活动可以操作相同的持久数据。
下面是说明我的伪代码:
使用Service:
/** Service maintaining state and performing background tasks */
class MyService extends Service {
Model mModel;
Binder mBinder;
onCreate() {
super.onCreate();
mBinder = new Binder();
// load mModel from disk, or do default initialization
}
onDestroy() {
super.onDestroy();
// save mModel to disk
}
onBind() {
return mBinder;
}
class Binder {
refresh() {
new AsyncTask() {
doInBackground() {
// update mModel from Internet
}
onPostExecute() {
sendBroadcasts(new Intent("my.package.REFRESHED"));
}
}.execute();
}
getState() {
return mModel.getState();
}
}
}
/** Activity displaying result */
class MyActivity extends ListActivity {
MyService.Binder mBinder;
onCreate() {
super.onCreate();
// register mReceiver
// bind service
}
onDestroy() {
super.onDestroy();
// unbind service
// unregister mReceiver
}
/** Invokes time-consuming update */
refresh() {
// binding is asynchronous, and user may trigger refreshing too early
if (mBinder != null) {
mBinder.refresh();
}
}
BroadcastReceiver mReceiver = new BroadcastReceiver() {
onReceive(Intent intent) {
if ("my.package.REFRESHED".equals(intent.getAction())
&& mBinder != null) {
updateViews(mBinder.getState());
}
}
};
}
将功能在自定义应用程序对象中全局访问。
/** Custom Application providing domain specific functionalities */
class MyApplication extends Application {
Model mModel;
onCreate() {
super.onCreate();
// load mModel from disk, or do default initialization
}
onTerminate() {
super.onTerminate();
// save mModel to disk
}
void refresh() {
/** time-consuming */
}
getState() {
return mModel.getState();
}
}
/** Activity displaying result */
class MyActivity extends ListActivity {
onResume() {
super.onResume();
// in case some top Activities have refreshed
// and user is navigating back
updateViews(((MyApplication)getApplicationContext()).getState());
}
/** Invokes time-consuming update */
refresh() {
new AsyncTask() {
doInBackground() {
((MyApplication)getApplicationContext()).refresh();
}
onPostExecute() {
// update the ListView according to result
updateViews(((MyApplication)getApplicationContext()).getState());
}
}.execute();
}
}
我认为“服务”方法的弱点是复杂性,因为绑定是异步的。而且很可能由于我同时拥有“ListActivity”和“Activity”,需要重复一些代码。
对于“应用程序”方法,文档指出不要依赖于调用“onTerminate()”。
我知道我很笨拙。解决这种问题的常规方法是什么?
非常感谢。