2010-08-17 2 views
0
Connexion

public class étend l'activité implémente Runnable {Faire face à cette discussion, gestionnaire et un message: « Redémarrer » un fil

  public static final int CONNECTION_ERROR = 1; 
      public static final int CONNECTION_DONE = 3; 



      @Override 
      public void onCreate(Bundle icicle) { 
       super.onCreate(icicle); 
       createConnection(); 
      } 

      public void createConnection() { 
       m_ProgressDialog = ProgressDialog.show(this, "Please wait...","Connection ...", true, false); 
       thread = new Thread(this); 
       thread.start(); 
      } 

      public void run() { 
       int i = connecTion(); 
       handler.sendEmptyMessage(i); 
      } 

      private Handler handler = new Handler() { 

       @Override 
       public void handleMessage(Message msg) { 
       if (msg.what == CONNECTION_ERROR) {     
         m_ProgressDialog.dismiss(); 

         AlertDialog.Builder alt_bld = new AlertDialog.Builder(thisA); 
         alt_bld.setMessage("Failed to connect to the server"); 
         alt_bld.setCancelable(false);          
         alt_bld.setNegativeButton("Quit",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {finish();}}); 
         alt_bld.setPositiveButton("Try Again",new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int id) { 
//HERE IS THE PROBLEM 
             /*m_ProgressDialog.show(thisA, "Please wait...", "Connection ...", true, false); 
             connecTion();*/ 
            } 
           }); 

         AlertDialog alert = alt_bld.create(); 
         alert.setTitle("ChatApp"); 
         alert.setIcon(R.drawable.icon); 
         alert.show(); 
        } 
        else {   
        m_ProgressDialog.dismiss(); 
        finish(); 
        } 
       } 
      }; 

      private int connecTion() {  
       /** Create a connection */   
       try { 

          //Function to create the connection (throwing error if there is a pb) 

       } catch (Exception e) { 
        Log.e("App","Failed to connect"); 
        return CONNECTION_ERROR; 
       }    
       //If no error left, everything is OK 
       return CONNECTION_DONE; 

      } 

Je veux réaliser un bouton « Try Again », qui lance à nouveau le fil pour créer la connexion et ProgressDialog en parallèle. Comment puis-je tuer le "vieux" thread et créer le nouveau correctement? Vaut-il mieux garder le même fil en vie et traiter uniquement avec Handler et Messages? Utiliser le service?

Merci!

Répondre

0

Vous pouvez configurer un filetage de pipeline; J'ai détaillé comment vous pouvez faire cela on my blog (Threading 5). Notez également qu'une fois qu'un thread est terminé, il ne peut plus être redémarré. Vous pouvez cependant garder un seul fil en vie et programmer le travail.

Une autre approche consiste à instancier un seul thread chaque fois que vous déchargez la tâche d'arrière-plan; ça va terminer et expirer - c'est la façon la plus simple de s'y prendre. Il pourrait être intéressant de regarder dans AsyncTask.

0

La façon dont je gère ce qui est habituellement par une classe d'aide qui étend Thread:

public class DoAyncStuff extends Thread 
{ 
    protected Handler mHandler; 

    public DoAyncStuff(Handler handler) 
    { 
     mHandler = handler; 
    } 

    public void run() 
    { 
     // Do async stuff here 
     // send message back once done 

     Message msg = Message.obtain(mHandler, CONNECTION_ERROR_OR_OTHER_MESSAGE); 
     mHandler.sendMessage(msg); 
    } 
} 

// to use 
DoAyncStuff asyncTask = new DoAyncStuff(mContext, mHandler) 
asyncTask.start(); 

// Then in your handler you can check to async task results and restart if needed 

    private Handler handler = new Handler() { 

      @Override 
      public void handleMessage(Message msg) { 
      if (msg.what == CONNECTION_ERROR_OR_OTHER_MESSAGE) { 
        // prompt to try again 
        // if trying again 
        DoAyncStuff asyncTask = new DoAyncStuff(handler) 
        asyncTask.start(); 
      } 
    } 
Questions connexes