我有一个问题,就是我用来控制一个线程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。 请帮我!。 感谢。
答案 0 :(得分: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;
答案 1 :(得分: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];
}
<强>编辑: - 强>
当您将第一个Argument声明为Integer但是您正在传递String时。 所以将它改为字符串,它将适用于。
public class conectar extends AsyncTask<String,Integer,Integer>