我有个问题,就是我用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。请帮我!。谢谢。