2

我有一个接受用户名和密码的登录页面。当用户单击“登录”时,会显示加载屏幕“正在登录..”,其中后台线程调用 doLogin() 方法,该方法调用 Web 服务并加载适当的页面。

我面临的问题是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

1 回答 1

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();               
    }               
};  
于 2012-08-15T10:11:30.140 回答