0

我想在几秒钟后取消将数据发送到服务器的 AsyncTask。我已经设置了 AsyncTask 和处​​理程序,但进度对话框仍在旋转。我怎样才能尽快干净地停止 AsyncTask。这就是我到目前为止所拥有的。提前致谢。

private class AsyncPostData extends AsyncTask<String, Void, String> {

    ProgressDialog progressDialog;
    String dateTimeScanned;

    @Override
    protected void onPreExecute()
    {


       // progressDialog= ProgressDialog.show(NfcscannerActivity.this, 
            //  "Connecting to Server"," Posting data...", true); 

        int buildVersionSdk = Build.VERSION.SDK_INT;
        int buildVersionCodes = Build.VERSION_CODES.GINGERBREAD;

        Log.e(TAG, "buildVersionSdk = " + buildVersionSdk 
                + "buildVersionCodes = " + buildVersionCodes);

        int themeVersion;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {

             themeVersion = 2;

        }else{

             themeVersion = 1;
        }

        progressDialog = new ProgressDialog(NfcscannerActivity.this, themeVersion);
        progressDialog.setTitle("Connecting to Server");
        progressDialog.setMessage(" Sending data to server...");
        progressDialog.setIndeterminate(true);

        try{
        progressDialog.show();
        }catch(Exception e){

            //ignore
        }
    };  


    @Override
    protected String doInBackground(String... params) {

        Log.e(TAG, "carerid in doinbackground = " + params[3] + " dateTimeScanned in AsyncPost for the duplecate TX = " + params[4]);

        dateTimeScanned = params[4];

        return nfcscannerapplication.loginWebservice.postData(params[0], params[1], params[2], params[3], params[4],
                params[5], params[6], params[7] + getVersionName(), params[8], params[9]);

    }

     @Override
        protected void onPostExecute(String result)
        {
         super.onPostExecute(result);

            try{
            progressDialog.dismiss();
            }catch(Exception e){
                //ignore
            }

            if( result != null && result.trim().equalsIgnoreCase("OK")  ){

                Log.e(TAG, "about to update DB with servertime");
                DateTime sentToServerAt = new DateTime();
                nfcscannerapplication.loginValidate.updateTransactionWithServerTime(sentToServerAt,null);
                nfcscannerapplication.loginValidate.insertIntoDuplicateTransactions(dateTimeScanned);

                tagId = null;
                tagType = null;
                tagClientId = null;

                //called to refresh the unsent transactions textview
                onResume();

            }else if(result != null && result.trim().equalsIgnoreCase("Error: TX duplicated")){
                Log.e(TAG, "response from server is Duplicate Transaction ");


                //NB. the following time may not correspond exactly with the time on the server
                //because this TX has already been processed but the 'OK' never reached the phone,
                //so we are just going to update the phone's DB with the DupTX time so the phone doesn't keep 
                //sending it.

                DateTime sentToServerTimeWhenDupTX = new DateTime();
                nfcscannerapplication.loginValidate.updateTransactionWithServerTime(sentToServerTimeWhenDupTX,null);

                tagId = null;
                tagType = null;
                tagClientId = null;



            }else{

                Toast.makeText(NfcscannerActivity.this,
                        "No phone signal or server problem",
                        Toast.LENGTH_LONG).show();
            }
        }

}//end of AsyncPostData 

.

public void initHandlerCancelAsyncPost(){

    cancelAsyncPostHandler = new Handler();
      cancelAsyncpostRunnable = new Runnable() {
            public void run() {

                cancelAsyncPost();

            }

            private void cancelAsyncPost() {

                apd.cancel(true);

            }
        };

    }

.

public void sendTransactionToWebservice(String statusForWbService){



        initHandlerCancelAsyncPost();
        cancelAsyncPostHandler.postDelayed(cancelAsyncpostRunnable, 8 * 1000);

        String[] params = new String[]{compID, tagId, tagClientId, carerID,
                formattedTagScanTime, formattedNowTime, statusForWbService, getDeviceName(), tagLatitude, tagLongitude}; 
        apd = new AsyncPostData();
        apd.execute(params);

    }
4

0 回答 0