2012-04-22 161 views
-1

我正在使用消息和處理程序來更改執行線程時顯示的processdialog的消息。代碼如下。問題與消息處理程序

final ProgressDialog processdialog = ProgressDialog 
     .show(MultiBootManager.this, EMPTY, 
       EMPTY, true); 
final Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.arg1) { 
     case 0: 
      System.out.println("msg0"); 
      processdialog 
        .setMessage(getString(R.string.formating) 
          + folderpath 
          + getString(R.string.cachext3)); 
     case 1: 
      System.out.println("msg1"); 
      processdialog 
        .setMessage(getString(R.string.formating) 
          + folderpath 
          + getString(R.string.dataext3)); 
     case 2: 
      System.out.println("msg2"); 
      processdialog 
        .setMessage(getString(R.string.formating) 
          + folderpath 
          + getString(R.string.systemext3)); 
     case 3: 
      System.out.println("endmsg"); 
      processdialog.dismiss(); 

     } 
    } 
}; 
Thread checkUpdate = new Thread() { 
    @Override 
    public void run() { 
     String[] shellinput = { EMPTY, 
       EMPTY, EMPTY, EMPTY, EMPTY }; 
     shellinput[0] = CMD_MKE2FS_EXT3; 
     shellinput[1] = folderpath; 
     if (cacheCheckBool) { 
      final Message m0 = new Message(); 
      m0.arg1 = 0; 
      handler.sendMessage(m0); 
      shellinput[2] = CACHE_IMG; 
      processManager 
        .inputStreamReader(
          shellinput, 20); 
     } 
     if (dataCheckBool) { 
      final Message m1 = new Message(); 
      m1.arg1 = 1; 
      handler.sendMessage(m1); 
      shellinput[2] = DATA_IMG; 
      processManager 
        .inputStreamReader(
          shellinput, 20); 
     } 
     final Message endmessage; 
     if (systemCheckBool) { 
      final Message m2 = new Message(); 
      m2.arg1 = 2; 
      handler.sendMessage(m2); 
      shellinput[2] = DATA_IMG; 
      processManager 
        .inputStreamReader(
          shellinput, 20); 
     } 
     endmessage = new Message(); 
     endmessage.arg1 = 3; 
     handler.sendMessage(endmessage); 
    } 
}; 
checkUpdate.start(); 

}

我遇到現在是該endmessage在操作完成之前發送的問題,ProcessDialog關閉在如0.5秒。如果所有Boolean都爲真,則在System.out中快速顯示msg1, msg2, msg3, emdmsg並關閉。爲什麼會這樣?我如何解決它?

回答

1

您需要爲每種情況添加中斷。否則,執行將轉入下一個案例。

public void handleMessage(Message msg) { 
    switch (msg.arg1) { 
    case 0: 
     System.out.println("msg0"); 
     processdialog 
       .setMessage(getString(R.string.formating) 
         + folderpath 
         + getString(R.string.cachext3)); 
     break; 
    case 1: 
     System.out.println("msg1"); 
     processdialog 
       .setMessage(getString(R.string.formating) 
         + folderpath 
         + getString(R.string.dataext3)); 
     break; 
    case 2: 
     System.out.println("msg2"); 
     processdialog 
       .setMessage(getString(R.string.formating) 
         + folderpath 
         + getString(R.string.systemext3)); 
     break; 
    case 3: 
     System.out.println("endmsg"); 
     processdialog.dismiss(); 
     break; 
    } 

最後突破並不是必需的,但它是一個很好的習慣,總是最終他們休息,因此,如果您以後添加一個新的情況下,你不必記得要休息加入之前的情況。

0

我不知道我明白 - 但你的代碼只是「發射」sendMessages真的沒有任何順序。所以特別是它貫穿每一個狀態,發出相關信息,然後 - 不管這些信息或其他地方發生什麼,它都會達到handler.sendMessage(endmessage); ,justz也會觸發這個。

現在,我看到一個「看門人」,檢查何時發送您的endMsg。

這是你的問題嗎?