2012-02-20 91 views
0

我想在這裏做的是,我想調用一個web服務,並基於它的響應,我可能會調用另一個web服務或啓動一個activity.I已經寫在一個web服務在一個單獨的線程,但問題是,我打電話在工作線程中的活動, 爲了讓自己更清楚我已經把我的僞代碼從一個線程開始一個android活動

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線程開始一個活動。這裏後端發生的過程是什麼(線程意義上的)?如果這不是一個好的做法什麼是其他替代方案來實現我的僞代碼

謝謝

回答

1

用消息發送到處理程序不在您的工作線程上的意圖。處理程序在UI線程上運行。如果他們不是,你將無法更新處理程序中的UI元素。

相關問題