2010-08-17 109 views
0

公共类连接扩展活动实现Runnable {与线程,处理程序和信息处理:“重新启动”线程

  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; 

      } 

我想实现一个“重试”按钮,重新启动线程创建连接和ProgressDialog并行。 如何杀死“旧”线程并正确创建新线程? 保持同一个线程处于活动状态并只处理Handler和Messages会更好吗?使用服务?

谢谢!

回答

0

你可以设置一个管道线程;我详细说明了你如何做到这一点on my blog (Threading 5)。另请注意,一旦线程完成,它不能再次重新启动。但是,您可以保留单个线程并安排工作。

另一种方法是每次卸载后台任务时实例化单个线程;它会完成并到期 - 这是更简单的方法。这可能是值得研究AsyncTask的。

0

我通常处理这个问题的方法是通过继承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(); 
      } 
    }