parameter-passing - 将参数传递给 asyntask

  显示原文与译文双语对照的内容
73 4

有一个错误,我用一次控制线程,只调用,但是现在我想做多次,但是现在我想打一次,但不知道怎么做,但是我不知道。 在这里我将保留代码:


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

}

我希望你传递给它的函数: 辅助和 aux1,请帮我。 谢谢。

时间: 原作者:

81 4

在异步任务中创建一个构造函数,将参数传递给它。 像这样

调用异步任务


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;

原作者:
88 5

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];


}

Edited:-

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


public class conectar extends AsyncTask<String,Integer,Integer>

原作者:
...