Set timeout function with AsyncTask [Android]

青春壹個敷衍的年華 提交于 2020-01-07 02:01:13

问题


In my case, I would like to get the button pressed and get process with the timeout. When button clicked then it will verify the accNo with web services, if the verification (ProgressDialog) is over 5 seconds then it will stop and display the alertDialog to notice user "Timeout".

But now I have not idea when I in testing with 1 milliseconds, in logically it will pause in alertDialog until get pressed, but now it will display the dialog in milliseconds then auto dismiss and intent to next activity. Here is my code:

@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();
        }
    }
}

回答1:


get() method will block the UI thread and I guess you don't want this.

You should implement cancel mechanics in doInBackground and call AsyncTask.cancel() by timeout [like that](https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long))

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

Be aware there are many gotchas with AsyncTask, check my article.



来源:https://stackoverflow.com/questions/38072241/set-timeout-function-with-asynctask-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!