0

在我的情況下,我想按下按鈕,並獲得超時的過程。點擊按鈕後,它將通過網絡服務驗證accNo,如果驗證(ProgressDialog)超過5秒,則停止並顯示alertDialog以通知用戶「超時」。使用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小時前我解決了這個問題。但還是謝謝你的回答。 –