(對不起,我的第一篇文章不太清楚)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