2013-04-11 4 views
1

J'ai créé une application Android sur laquelle un gestionnaire de clics sur un bouton activera toutes les 2 secondes un grille-pain apparaîtra. même de l'autre façon que j'ai un bouton d'arrêt dont je veux arrêter le fil conducteur sur cliquantcomment arrêter un gestionnaire dans android

C'est mon code

private final int FIVE_SECONDS = 2000; 


    public void scheduleSendLocation() { 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       sendLocation();   // this method will contain your almost-finished HTTP calls 
       handler.postDelayed(this, FIVE_SECONDS); 
      } 
     }, FIVE_SECONDS); 
    } 


    protected void sendLocation() { 
     Toast.makeText(getApplicationContext(), "Your Location is ", Toast.LENGTH_SHORT).show(); 
    } 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 
     stop = (Button) findViewById(R.id.stop); 

     btnShowLocation.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) {   
       scheduleSendLocation(); 

      } 
     }); 

     stop.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) {   
     // what to write here 
      } 
     }); 

    } 

Répondre

5

Essayez d'appeler la code suivant

stop.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) {   
      handler.removeCallbacks(runnable); 
     } 
    }); 

runnable est le runnable que vous avez ajouté en appeler postDelayed. Tous les messages en attente de runnable seront supprimés dans la file d'attente des messages.

+0

monsieur quel est l'argument pour removeCallbacks() –

+0

comment vais-je faire cela .... je ne reçois pas –

+0

S'il vous plaît vérifier la réponse mise à jour. –

0

Essayez ceci:

stop.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) {   
      myHandlerThread.interrupt(); 
      myHandlerThread = null; 
     } 
    }); 
Questions connexes