(对不起,我的第一篇文章不太清楚)Android中的MVC:异步更新的应用程序或服务?
这里是情况:我有要从互联网刷新的数据。我们称之为Model
。
我想要做什么:基本上,它听起来像一个MVC模型,其中Model
也保持在本地(私人)存储持久。 Model
及其相关方法在应用方面是明智的。有几个Activity
的是显示和操作其不同方面:
- 用户 可前往[在不同
Activity
的 ,从不同角度显示Model
。目前,我有一个ListActivity
所有元素,一个元素的细节Activity
- 有时
Model
需要清爽。 当然这是在不同的线程上完成的。刷新可以从几个Activity
的触发。 - 有几个(耗时),可以从不同的
Activity
的 - 我的应用程序加载被触发,并保存
Model
私人存储在启动时 并停止
我的问题共同 任务:我不确定把Model
和相关的任务放在哪里。另外,我不知道用什么机制来通知Activity
的。目前我拿出2种方法:
- 使用
Service
并发送广播。保存到磁盘在Service#onDestroyed()
中执行,所以我想通过将它绑定到Activity
来最小化。在这一点上,我也不知道如何传递更新信息:是否提供获得者Binder
,或将其包括在广播消息中。 - 自定义
Application
对象,以便更新方法和获取器可在全球范围内使用。然后我使用AsyncTask
执行从Activity
的更新。如果还有其他Activity
位于当前的Activity
之后,那么当用户返回时,它们将在onResume()
中更新。我不使用一类具有静态方法
原因:
- 我需要保存和存储
Model
到磁盘。 - 一些方法需要一个Context 显示祝酒词,通知,缓存等
而且,我不把这些功能在Activity
因为有操纵同一块几项活动持久数据。
下面是伪代码说明我的意思:
使用服务:
/** 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();
}
}
弱点,我可以考虑为Service
approa ch是复杂的,因为绑定是异步的。它很可能是我要重复一些代码,因为我有两个ListActivity
和Activity
对于Application
方法,该文件说,不要依靠onTerminate()
被调用。
我知道我很尴尬。传统的解决这类问题的方法是什么?
非常感谢。
这是我的错,因为不清楚。我有几个活动,访问和变异的应用程序相同的一块数据:) – Phil 2010-07-03 14:34:28
我编辑我的帖子。希望它不太罗嗦。 – Phil 2010-07-03 14:34:52