2014-09-23 72 views
2

我有一個按鈕,點擊時調用一個啓動線程的方法。但是那個線程有停止,很好。我想要先完成該線程然後執行其餘的代碼。試圖停止執行2秒

我試圖使用wait()但不工作。

public void configure(Button arg0,TextView arg1) throws InterruptedException{ 

    //calling OpenDeviceListener 
    readText=arg1; 
    button= arg0; 

    //handler 
    final Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      Log.d("Handler","running"); 
      if (actualNumBytes != 0x00) { 
       Log.d("handler","if"); 
       readText.append(String.copyValueOf(readBuffer, 0, 
         actualNumBytes)); 
       Log.d("handler","if 312"); 
       actualNumBytes = 0; 
       Log.d("handler","if end"); 
      } 
      Log.d("handler","closed"); 
     } 
    }; 


    Log.d("163","tab"); 
    uartInterface = new CH34xAndroidDriver(
      (UsbManager) getSystemService(Context.USB_SERVICE), this, 
      ACTION_USB_PERMISSION); 
    Log.d("167","tab"); 
    act_string = getIntent().getAction(); 
    if(-1 != act_string.indexOf("android.intent.action.MAIN")) 
    { 
     Log.d(TAG, "android.intent.action.MAIN 171"); 
    } 
    else if(-1 != act_string.indexOf("android.hardware.usb.action.USB_DEVICE_ATTACHED")) 
    { 
     Log.d(TAG, "android.hardware.usb.action.USB_DEVICE_ATTACHED 175"); 
    } 

    if(!uartInterface.UsbFeatureSupported()) 
    { 
     Toast.makeText(this, "No Support USB host API", Toast.LENGTH_SHORT).show(); 
     Log.d("182","tab"); 
     readText.setText("No Support USB host API"); 
     Log.d("184","tab"); 
     uartInterface = null; 
    } 

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    if(READ_ENABLE == false) { 
     READ_ENABLE = true; 
     Log.d("192","tab"); 
     handlerThread = new readThread(handler); 
     handlerThread.start(); 
    } 
    Log.d("192","End"); 
    try{ 
    this.wait(100); 
    } 
    catch(InterruptedException e){ 
     e.printStackTrace(); 
     Log.d("Exception",e.getMessage()); 
    } 
    // TODO Auto-generated method stub 
       Log.d("205","OpenDeviceListener"); 
       boolean flags; 
       if(false == isConfiged) { 
        Log.d("209","OpenDeviceListener"); 
        Toast.makeText(global_context,""+((Boolean)uartInterface.isConnected()), Toast.LENGTH_LONG).show(); 
        isConfiged = true; 
      //  writeButton.setEnabled(true); 
        if(uartInterface.isConnected()) { 
         Toast.makeText(global_context,""+(Boolean)uartInterface.isConnected(), Toast.LENGTH_SHORT).show(); 
         Log.d("213","OpenDeviceListener"); 
         flags = uartInterface.UartInit(); 
         if(!flags) { 
          Log.d(TAG, "Init Uart Error 2"); 
          Toast.makeText(global_context, "Init Uart Error", Toast.LENGTH_SHORT).show(); 
         } else { 
          if(uartInterface.SetConfig(baudRate, dataBit, stopBit, parity, flowControl)) { 
           Log.d(TAG, "Configed"); 
          } 
         } 
        } 

       } 
} 

和PLZ清除一個事情stop()不被調用的線程,直到線程將運行。 感謝

回答

0
final Runnable runnable = new Runnable() { 

         @Override 
         public void run() { 
//this method will be called after 2 seconds 
          //do your task here 
         } 
        }; 
        final Handler h = new Handler(); 
        h.removeCallbacks(runnable); // cancel the running action (the 
                // hiding process) 
        h.postDelayed(runnable, 2000); 
+0

爲什麼wait()有沒有在這裏工作,任何想法 – 2014-09-23 03:51:32

0

試試這個:

Object lock = new Object; 
synchronized(lock){ 
    lock.wait(); 
} 

這將阻止當前線程中運行這些代碼。 當它的時間通知受阻代碼

synchronized (lock) { 
    lock.notify(); 
} 

如果只想睡覺線程在特定的時間,爲什麼不使用

Thread.sleep(1000); 
+0

沒有我不想停止線程,我想停止執行威脅調用後的主代碼。我應該只是放一個新的線程類,並使用wait()在新線程中運行它。 – 2014-09-23 04:57:57

+0

在你的主線程中啓動一個線程首先使用thread.start()運行它;然後thread.join();那麼主線程將在線程完成後繼續運行。 – SalutonMondo 2014-09-23 06:35:53