[英]Starting an android activity from a thread
我要在這里做的是我想調用一個Web服務,並根據其響應我可能調用另一個Web服務或啟動一個活動。我已經在一個單獨的線程中編寫了Web服務,但是問題是我正在調用為了使自己更加清楚,我將自己的偽代碼放在了工作線程中。
if (User ID and Password present in the shared preference) THEN
Utils.checkauthorisation(API) //Web Service Call
if(respsonse is Paswordexpired)
erase password from DB
Goto (LOGIN SCREEN)
else if( download of images hasn't happened today) ) THEN
UTILS.DownloadImages//Web service call
if(response==connectivityorOtherError)
Toast respective Message
GOTO (GALLERY SCREEN)
else if (response==confilicted Data)
Goto (CHANGES SCREEN)
endif
endif
endif
我打算顯示一個進度條,並在這樣的線程中執行所有這些事件
progressDialog = ProgressDialog.show(this, "Loading",
"Authenticating Please wait.");
new Thread() {
public void run() {
///execute the pseudo code
Message msg = Message.obtain();
msg.what = 1;
messagHandler.sendMessage(msg);
}
}.start();
private static Handler messagHandler = new Handler() {
public void handleMessage(Message message) {
super.handleMessage(message);
switch (message.what) {
case 1:
progressDialog.dismiss();
break;
default:
break;
}
}
};
但令我不安的是,我必須在此處的工作線程中啟動活動 。 這是好習慣嗎? 我最初以為我們只能從UI線程開始活動。 在此后端(線程意義上)正在發生什么過程? 如果這不是一個好習慣,那么還有什么其他方法可以實現我的偽代碼呢?
謝謝
通過不在工作線程上的消息向處理程序啟動意圖。 處理程序在UI線程上運行。 如果不是,則將無法更新處理程序中的UI元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.