0

我有个问题,就是我用AsynTask来控制一个线程,而且我只调用了一次AsynTask,但是现在我想做的就是调用不止一次,当然为此我要花几个参数,但是不要知道怎么做,我已经尝试了数千种方法但不起作用。在这里我留下我的代码:

     public class leer_registros extends Activity {
    TextView text, depurar;
    EditText reg,ip;
    Button boton;
    float resultado;
    AccionesScada accionesScada = new AccionesScada();


    InetAddress addr = null;        //direccion del esclavo
    int port = Modbus.DEFAULT_PORT;//puerto por defecto 502 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.lblRegistro);
        ip = (EditText) findViewById(R.id.txtIp);
        reg = (EditText) findViewById(R.id.txtReg);
        depurar = (TextView) findViewById(R.id.txtdepurar); 
        boton = (Button)findViewById(R.id.btnVerRegistro);
        depurar.setTextColor(Color.RED);
    }
    public class conectar extends AsyncTask<Void,Void,Void>{
        int m;
        int startReg;
        int count;
        String dirIP;
        String dirReg;



        public conectar(String aux,String aux1) {
        this.dirIP=aux;
            this.dirReg=aux1;


            try {

                //IP address;
                //aux1 = ip.getText().toString();
                addr = InetAddress.getByName(dirIP);
                //aux = reg.getText().toString();
                startReg = Integer.decode(dirReg).intValue();
                depurar.setText(dirIP);
                //Reg = startReg[0];

            } catch (Exception e) {
                Log.d("MODBUS","IP error", e);

            }
        }

         protected  Void doInBackground(Void... urls) {



             m = accionesScada.conectar(addr, port);
             resultado = accionesScada.LeerRegistro(startReg, count);

             return null;


         }


         protected void onPostExecute(Integer bytes) {
             if(m==1){
                 depurar.setText("conectado!!!");
                 depurar.setTextColor(Color.GREEN);
            }
                 String resul = String.valueOf(resultado);
                 text.setText(resul); 
         }
    }


    public void verRegistro(View v)  {
        final Handler handler = new Handler();
        Timer timer = new Timer();

        //IP address;
         final String aux = ip.getText().toString();
         final String aux1 = reg.getText().toString();
        TimerTask doAsynchronousTask = new TimerTask() {
        @Override

        public void run() {
            handler.post(new Runnable() {

                @Override
                public void run() {
                    try{ 
                         depurar.setText(aux1);
                        /*conectar conectamos = new conectar();
                        conectamos.execute(aux,aux1);*/
                        conectar conectamos = new conectar(aux,aux1);
                        conectamos.execute();

                    }catch(Exception e){
                        Log.d("MODBUS", "Error Timertask");
                    }

                }

            });

        }

        }; 
        timer.schedule(doAsynchronousTask, 0, 1000);
    }

}

我希望您将其传递给函数:aux 和 aux1。请帮我!。谢谢。

4

2 回答 2

1

在 Async Task 中创建一个构造函数以将参数传递给它。像这样

调用异步任务

new conectar(url,_payload,method,context).execute();

带有构造函数的 AsycTask

private static class Conectar extends
            AsyncTask<Integer,Integer,Integer> {


        private final String mUrl;
        private final String mPayload;
        private final HttpMethod mMethod;
        private final Context mContext;

        public Conectar(String url,String _payload, HttpMethod method,final Context context) {

            mContext = context;
            mUrl=url;
            mPayload=_payload;
            mMethod=method;
        }

        @Override
        protected Integer doInBackground(Void... vParams) {
            ResponseWrapper user = null;
            String url = mUrl;
于 2013-10-09T06:57:06.297 回答
0
conectar conectamos = new conectar();
conectamos.execute(1, 2, 3);// Pass the parameter like this



// Get the parameter like this
protected Integer doInBackground(Integer... urls) {
  int parameter1=urls[0];
  int parameter1=urls[1];
  int parameter1=urls[2];


}

编辑:-

当您将第一个参数声明为整数但您正在传递字符串时。所以把它改成字符串,它就可以了。

public class conectar extends AsyncTask<String,Integer,Integer>
于 2013-10-09T06:56:31.933 回答