0

제 경우에는 버튼을 눌렀는데 시간 초과가있는 프로세스를 얻고 싶습니다. 버튼을 클릭하면 확인 (ProgressDialog)이 5 초 이상 경과하면 alertDialog를 중지하고 표시하여 사용자 "시간 초과"를 알리는 웹 서비스의 accNo를 확인합니다.AsyncTask [Android]로 시간 초과 기능 설정

하지만 지금은 1 밀리 초로 테스트 할 때 논리적으로는 alertDialog에서 일시 중지됩니다. 그러나 밀리 초 단위로 대화 상자를 표시 한 다음 자동으로 닫고 다음 활동을 시도합니다. 여기 내 코드는 다음과 같습니다.

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_information3); 

btn_next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (title.getText().toString().equals("INFO")) { 
       InfoAsyncTask infoAsyncTask = new InfoAsyncTask(); 
       try { 
        infoAsyncTask.execute().get(1, TimeUnit.MILLISECONDS); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        e.printStackTrace(); 
       } catch (TimeoutException e) { 
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(Information3.this); 
        alertDialog.setTitle(":: Error ::"); 
        alertDialog.setMessage("Verify Timeout. Please try again."); 

        alertDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
         } 
        }); 
        alertDialog.show(); 
       } 
      } 
     }); 
    } 

private class InfoAsyncTask extends AsyncTask<Void, Void, String> { 
    private String results; 
    private ProgressDialog pDialog; 
    private Object resultRequestSOAP; 
    String accNo = et_accNo.getText().toString().trim(); 
    int timeout = 15000; 

    @Override 
    protected void onPreExecute() { 
     pDialog = new ProgressDialog(Information3.this); 
     pDialog.setMessage("Verifying..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("Code", InfoCode); 
     request.addProperty("AccNo", accNo); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout); 
     androidHttpTransport.debug = true; 

     try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      String requestDumpString = androidHttpTransport.requestDump; 
      Log.d("request Dump: ", requestDumpString); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     try { 
      resultRequestSOAP = envelope.getResponse(); 
      results = resultRequestSOAP.toString(); 
      Log.d("Output: ", results); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String result) { 
     if (resultRequestSOAP == null) { 
      if (pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } else { 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(Information3.this); 

       alertDialog.setTitle(":: Error ::"); 
       alertDialog.setMessage("Connection error, please check your internet connection."); 

       alertDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
       alertDialog.show(); 
       pDialog.dismiss(); 
      } 
     } else { 
      if (results.equals("false")) { 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(Information3.this); 

       alertDialog.setTitle(":: Warning ::"); 
       alertDialog.setMessage("Please fill in correct account number"); 

       alertDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
       alertDialog.show(); 
      } else if (et_billNo.getText().toString().trim().isEmpty()) { 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(Information3.this); 

       alertDialog.setTitle(":: Warning ::"); 
       alertDialog.setMessage("Please fill in bill number"); 

       alertDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
       alertDialog.show(); 
      } else if (et_amountNo.getText().toString().trim().isEmpty() || Integer.parseInt(et_amountNo.getText().toString().trim()) <= 0) { 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(Information3.this); 

       alertDialog.setTitle(":: Warning ::"); 
       alertDialog.setMessage("Please fill in amount"); 

       alertDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
       alertDialog.show(); 
      } else if (results.equals("true")) { 
       Title = title.getText().toString(); 
       String value1 = et_accNo.getText().toString().trim(); 
       String value2 = et_billNo.getText().toString().trim(); 
       int value3 = Integer.parseInt(et_amountNo.getText().toString().trim()); 

       addSearchInput(value1); 
       addSearchInput(value2); 
       addSearchInput(String.valueOf(value3)); 

       Intent intent = new Intent(Information3.this, confirmation3.class); 
       intent.putExtra("name", Title); 
       intent.putExtra("value1", value1); 
       intent.putExtra("value2", value2); 
       intent.putExtra("value3", value3); 
       startActivity(intent); 
      } else { 
       super.onPreExecute(); 
      } 
      pDialog.dismiss(); 
     } 
    } 
} 

답변

0

get() 메서드는 UI 스레드를 차단하고 사용자가 원하지 않는 것 같습니다.

당신은 doInBackground에서 역학을 취소 구현하고 (긴 https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable) [그런] 시간 제한에 의해 AsyncTask.cancel()를 호출한다)

final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
     AsyncTask.cancel(); 
     } 
    }, 1); 

하는 AsyncTask를 많은 개는 거기에주의, 내 article를 확인합니다.

+0

감사합니다. @Maxim, 5 시간 전에이 문제를 해결했습니다. 그러나 아직도 당신의 대답에 감사드립니다. –