0

Hi good I have the following problem: I have several toggle buttons and when I click on one call AsyncTask to do its function, but the problem is that when I go back and click the button to toggle this off I have to stop that thread, and I try with the variable I have called "flag" but does not assign variables as well when it is assigned in the AsyncTask not assign it well and gets the assignment to do at the beginning of the code when I check if any toggle button is selected.

here you have the code:

int falg;

...



btnBoton.setOnClickListener(new View.OnClickListener() {

   public void onClick(View arg0)

   {

       if(btnBoton.isChecked()){

       salir = false;

       flag = 1;

           verRegistro();}

       else{

        salir = true;

        flag = 0;

        }

       //accionesScada.desconectar();

   }

});



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

   int m;

   int startReg;

   int count;

   String dirIP=new String();

        String dirReg=new String();

        TextView texto,conectado;



        public conectar(String ip,String reg,TextView t,TextView r) {

this.dirIP=ip;

            this.dirReg=reg;

            this.conectado = t;

            this.texto = r;

            try {

   //IP address;



addr = InetAddress.getByName(dirIP);

startReg = Integer.decode(dirReg).intValue();



            } catch (Exception e) {

   Log.d("MODBUS","IP error", e);

        }

       }



   protected Integer doInBackground(String... urls) {

   m = accionesScada.conectar(addr, port);

   resultado = accionesScada.LeerRegistro(startReg, count);



   return 0;

         }





   protected void onPostExecute(Integer bytes) {





   if(btnBoton.isChecked() == false){

   setFlag(0);

        salir = true;

        //depurar2.setText(String.valueOf(flag));

        depurar.setText("Desconectado");

        depurar.setTextColor(Color.RED);

        this.cancel(true);

        }else{

        if(m==1){

    conectado.setText("Conectado");

    conectado.setTextColor(Color.GREEN);

 }

        String resul = String.valueOf(resultado);

      texto.setText(resul);

        }





   }

    }

    public void setFlag(int bandera){

   this.abc = bandera;

    }

    public int getFlag (){

   return flag;

    }

//tenemos varios tipos de ver registro uno para cada registro que podamos ver simultaneamente en pantalla, para eso esta.

public void verRegistro()  {

final Handler handler = new Handler();

   Timer timer = new Timer();

   TimerTask doAsynchronousTask = new TimerTask() {

   @Override

public void run() {

handler.post(new Runnable() {



@Override

public void run() {







       //accionesScada.desconectar();





try{ 

String aux = new String();

   String aux1 = new String();

   try {

   //IP address;



   aux1 = plc1;

   Cursor Creg = (SQLiteCursor)reg.getSelectedItem();

   aux = obtenerDireccionRegistro(Creg.getString(0));





} catch (Exception e) {

   Log.d("MODBUS","IP error", e);



   }

conectar conectamos = new conectar(aux1,aux,depurar,text);

       conectamos.execute("");







}catch(Exception e){

Log.d("MODBUS", "Error Timertask");

}





}



});



}



   }; 



 //here is the problem, here the flag always what made mark the beginning and at the aynctask not assigned either to the flag value, and do not know why always so here is sending commands to the AsyncTask continuously every second and I I want is to cancel this.


       if(flag == 0){

       timer.cancel();

           depurar2.setText(String.valueOf(flag));}

       else if(flag == 1){

       depurar2.setText(String.valueOf(flag));

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



        }

Thanks for your help =).

Isma9
  • 599
  • 1
  • 8
  • 14
  • http://stackoverflow.com/questions/4748964/android-cancel-asynctask-forcefully – Ali Imran Oct 28 '13 at 08:03
  • What I want is to cancel the timer! because if I kill the timer therefore killed the AsyncTask, but my problem is that the global variable that I have to control that which is the variable called "flag" is not assigned correctly the number that must be assigned as always recognize one and does not change to zero when the button was off toogle – Isma9 Oct 28 '13 at 08:14

0 Answers0