2

ユーザー名とパスワードを受け入れるログインページがあります。ユーザーが「ログイン」をクリックすると、ロード画面「サインイン..」が表示され、Webサービスを呼び出して適切なページをロードするdoLogin()メソッドを呼び出すバックグラウンドスレッドが表示されます。

私が直面している問題は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 に答える