如何在asyncTask中调用处理程序

时间:2017-03-31 06:26:17

标签: java android

class GET_DOC_CONTACT_DELAY_TIME extends AsyncTask<String, String, String> {

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

        }

        protected String doInBackground(String... args) {


            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("dhw_gp_id",String.valueOf(Login.reg_id)));
            params.add(new BasicNameValuePair("dreg_id",String.valueOf(E_patId)));
            params.add(new BasicNameValuePair("dslno",String.valueOf(E_slno)));
            params.add(new BasicNameValuePair("ddos",String.valueOf(dosString)));
            params.add(new BasicNameValuePair("ddocid",String.valueOf(doc_id)));
            params.add(new BasicNameValuePair("dimei",String.valueOf(Login.imei)));

            JSONObject json = jsonParser.makeHttpRequest(DOC_SMS, "POST", params);

            final String dm = doc_contaclist.get(current_doc).get("vdocmobile");
            final String delay = doc_contaclist.get(current_doc).get("vdoctime");

            // check for success tag
            try {
                 rsuccess = json.getInt(TAG_RMSUCCESS);

                if (rsuccess == 1) {

                    runOnUiThread(new Runnable() {
                        public void run() {

                            final String message = "Patient "  + patFirstName + " " + patLastName +" with Id : "+ E_patId + "\n"+" is having an emergency and needs diagnosis immediately,";
                            SmsManager.getDefault().sendTextMessage(String.valueOf(dm), null,message, null, null);

                            new AlertDialog.Builder(PdfView.this)
                                    .setTitle("Confirmation Message")
                                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {

                                            Intent i = new Intent(PdfView.this,Patient_Register.class);
                                            i.putExtra("clear_text",status_info);
                                            startActivity(i);
                                        }
                                    })
                                    .setMessage("ECG Sent to Doctor")
                                    .setIcon(R.drawable.alert1)
                                    .show();

                        }
                    });
                } else if (rsuccess == 0) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast toast = Toast.makeText(PdfView.this, "Required fields missing", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();

                        }
                    });
                }else if (rsuccess == 2) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast toast = Toast.makeText(PdfView.this, "Database selection failed", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();

                        }
                    });
                }else if (rsuccess == 3) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast toast = Toast.makeText(PdfView.this, "Failed to insert", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();

                        }
                    });
                }else if (rsuccess == 4) {

                    runOnUiThread(new Runnable() {
                        public void run() {

                            current_doc++ ;

                             Toast toast = Toast.makeText(PdfView.this, "SMS to next doctor", Toast.LENGTH_SHORT);
                             toast.setGravity(Gravity.CENTER,0,0);
                             toast.show();

                       }
                   });
                }  else if (rsuccess == 5) {
                    runOnUiThread(new Runnable() {
                        public void run() {

                            loop_exit= true;

                            Toast toast = Toast.makeText(PdfView.this, "Processed", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();

                        }
                    });
                }

          Handler handler = new Handler();
       handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Send_sms();
            }
       }, 20000); 



            } catch (JSONException e) {
                e.printStackTrace();
            }



            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            if ((pDialog != null) && pDialog.isShowing()) {
                pDialog.dismiss();
            }
        }
    }

尝试在asyncTask方法中调用处理程序,但它抛出错误(应用程序出现问题)。应该以1分钟或2分钟的延迟调用此处理程序。试过多种方法。任何人请帮我在asynctask里面调用处理程序。

2 个答案:

答案 0 :(得分:1)

根据您在 Handler 方法中的要求致电 onPostExecute

  

onPostExecute =&gt;在doInBackground(Params ...)之后在UI线程上运行。

protected void onPostExecute(String file_url) {

     Handler handler = new Handler();
     handler.postDelayed(new Runnable()
        {
        @Override
        public void run() 
           {
            Send_sms();
           }
        }, 20000); //Time Delay
      }

答案 1 :(得分:0)

确保在Handler之外创建AsyncTask,可能在ActivityFragment您正在启动任务。您可以将Handler作为参数传递给AsyncTask。 通过这种方式,处理程序绑定到MainThread,甚至可以在UI-Thread上发布更改。