2012-08-15 38 views
2

我有一個登錄頁面,它接受用戶名爲&的密碼。當用戶點擊「登錄」時,會顯示一個加載屏幕「正在登錄..」,後臺線程將調用一個調用Web服務並加載相應頁面的doLogin()方法。登錄頁面上的黑莓 - Dialog.cancel()

我面臨的問題是點擊登錄後「取消」登錄Dialog.cancel()。它取消加載屏幕,但後臺線程仍在處理中,即使點擊「取消」後,新頁面也會加載。我怎樣才能取消後臺線程?

Thread backgroundWorker = new Thread(new Runnable() { 
    public void run() { 
     doLogin(uname, pwd); 
    } 
}); 
Dialog busyDialog = new Dialog("Signing in...", 
           new String [] { "Cancel" }, 
           new int [] { Dialog.CANCEL}, 
           Dialog.CANCEL, 
           Bitmap.getPredefinedBitmap(Bitmap.HOURGLASS)) 
{ 
    public void fieldChanged(Field field1, int context1) 
    { 
     //Something to stop the login and close the loading screen 
    } 
}; 
busyDialog.setEscapeEnabled(false); 
busyDialog.show(); 
backgroundWorker.start(); 

而且它被調用該方法推新畫面doLogin()

private String doLogin(String user_id, String password) 
{ 
    SoapObject resultRequestSOAP = null; 
    HttpConnection httpConn = null; 
    HttpTransport httpt; 
    SoapPrimitive response = null; 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    request.addProperty("username", user_id); 
    request.addProperty("password", password); 
    System.out.println("The request is=======" + request.toString()); 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 
    httpt = new HttpTransport(URL+C0NNECTION_EXTENSION); 
    httpt.debug = true; 
    try 
    { 
     httpt.call(SOAP_ACTION, envelope); 
     response = (SoapPrimitive) envelope.getResponse(); 
     String result = response.toString(); 
     resultRequestSOAP = (SoapObject) envelope.bodyIn; 
     String[] listResult = split(result, sep); 
     strResult = listResult[0].toString(); 
     if(strResult.equals("credentialdenied")) 
     { 
      Dialog.alert("Invalid login details."); 
     } 
     else 
     { 
      strsessionFirstName = listResult[1].toString(); 
      strsessionLastName = listResult[2].toString(); 
      strsessionPictureUrl = MAINURL + listResult[3].substring(2); 
      strsessionStatusId = listResult[4].toString(); 
      strsessionStatusMessage = listResult[5].toString(); 
      strsessionLastUpdateTst = listResult[6].toString(); 
     } 
     if(strResult.equals("credentialaccepted")) 
     { 
      if(checkBox1.getChecked() == true) 
      { 
       persistentHashtable.put("username", user_id); 
       persistentHashtable.put("password", password); 
      } 
      Bitmap bitmap = getLiveImage(strsessionPictureUrl, 140, 140); 
      nextScreen.getUsername(user_id); 
      nextScreen.getPassword(password); 
      nextScreen.setPictureUrl(bitmap); 
      nextScreen.setImage(strsessionPictureUrl); 
      nextScreen.setFirstName(strsessionFirstName, strsessionLastName, strsessionLastUpdateTst, strsessionStatusMessage); 
      UiApplication.getUiApplication().invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        UiApplication.getUiApplication().pushScreen(nextScreen); 
       } 
      }); 
     } 
    } catch (IOException e) { 
     System.out.println("The exception is IO==" + e.getMessage()); 
    } catch (XmlPullParserException e) { 
     System.out.println("The exception xml parser example===" 
      + e.getMessage()); 
    } 
    System.out.println(resultRequestSOAP); 
    return response + ""; 
} 

回答

4

您可能需要一個boolean成員變量添加到您的課,你檢查的多個步驟,你doLogin()方法中:

private boolean stopRequested = false; 

private synchronized void requestStop() { 
    stopRequested = true; 
} 

private synchronized boolean isStopRequested() { 
    return stopRequested; 
} 

private String doLogin(String user_id, String password)   
{   
    SoapObject resultRequestSOAP = null;   
    HttpConnection httpConn = null;   
    HttpTransport httpt;   
    SoapPrimitive response = null;   
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);   
    request.addProperty("username", user_id);   
    request.addProperty("password", password);   
    System.out.println("The request is=======" + request.toString());   
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);   
    envelope.dotNet = true;   
    envelope.setOutputSoapObject(request);   
    httpt = new HttpTransport(URL+C0NNECTION_EXTENSION);   
    httpt.debug = true; 

    if (isStopRequested()) return null;   

    try   
    {   
     httpt.call(SOAP_ACTION, envelope);   
     response = (SoapPrimitive) envelope.getResponse();   
     String result = response.toString();   
     resultRequestSOAP = (SoapObject) envelope.bodyIn;   
     String[] listResult = split(result, sep);   
     strResult = listResult[0].toString();   
     if(strResult.equals("credentialdenied"))   
     {   
      Dialog.alert("Invalid login details.");   
     }   
     else   
     {   
      strsessionFirstName = listResult[1].toString();   
      strsessionLastName = listResult[2].toString();   
      strsessionPictureUrl = MAINURL + listResult[3].substring(2);   
      strsessionStatusId = listResult[4].toString();   
      strsessionStatusMessage = listResult[5].toString();   
      strsessionLastUpdateTst = listResult[6].toString();   
     }  

     if (isStopRequested()) return null;   

     if(strResult.equals("credentialaccepted"))   
     {   
      if(checkBox1.getChecked() == true)   
      {   
       persistentHashtable.put("username", user_id);   
       persistentHashtable.put("password", password);   
      }   
      Bitmap bitmap = getLiveImage(strsessionPictureUrl, 140, 140);   
      nextScreen.getUsername(user_id);   
      nextScreen.getPassword(password);   
      nextScreen.setPictureUrl(bitmap);   
      nextScreen.setImage(strsessionPictureUrl);   
      nextScreen.setFirstName(strsessionFirstName, strsessionLastName, strsessionLastUpdateTst, strsessionStatusMessage); 

      if (isStopRequested()) return null;   

      UiApplication.getUiApplication().invokeLater(new Runnable()   
      {   
       public void run()   
       {   
        UiApplication.getUiApplication().pushScreen(nextScreen);   
       }   
      });   
     }   
    } catch (IOException e) {   
     System.out.println("The exception is IO==" + e.getMessage());   
    } catch (XmlPullParserException e) {   
     System.out.println("The exception xml parser example==="   
      + e.getMessage());   
    }   
    System.out.println(resultRequestSOAP);   
    return response + "";   
}   

然後,停止這樣的線程:

Dialog busyDialog = new Dialog("Signing in...",    
           new String [] { "Cancel" },    
           new int [] { Dialog.CANCEL},    
           Dialog.CANCEL,    
           Bitmap.getPredefinedBitmap(Bitmap.HOURGLASS))    
{    
    public void fieldChanged(Field field1, int context1)    
    {    
     requestStop();    
    }    
}; 
+0

這樣一個全面的答案。謝謝! – Sarah 2012-08-16 04:39:37

+0

@Sarah,不客氣。順便說一下,我確實使用'backgroundWorker.interrupt()'查看了一種替代方法來殺死線程的運行代碼,但在查看了更多文檔後,這看起來並不可靠/穩定。所以,我認爲你只需要做一些類似於我上面發佈的內容,然後根據需要在'doLogin()'中的任意位置放置'if(isStopRequested())'返回null;'。 – Nate 2012-08-16 04:42:50

+0

是在發佈我的問題之前,我曾經玩過代碼並嘗試過backgroundWorker.interrupt()。但是,這並不適合我。 – Sarah 2012-08-16 04:51:50