BlackBerry - 登录页面上的Dialog.cancel()

时间:2012-08-15 09:40:56

标签: blackberry dialog listener

我有一个接受用户名和用户名的登录页面密码。当用户单击“登录”时,将显示加载屏幕“登录...”,后台线程调用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 + "";
} 

1 个答案:

答案 0 :(得分: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();               
    }               
};