2013-05-03 73 views
-3

我試圖創建一個語音識別應用程序,其中應用程序接收語音和發送的東西。我想要調用onEndOfSpeech方法等待一秒鐘,然後再重新執行整個語音識別意圖。在Java中使用延遲

public void onEndOfSpeech() { 
    Log.d("Speech", "onEndOfSpeech"); 

     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 
     } 

不確定我是否正確地做到了這一點。 謝謝!

+0

了Thread.sleep(3000);不好。使用處理程序。 – Raghunandan 2013-05-03 05:36:39

+0

最好從現在開始計劃3000毫秒發生,而不是將整個程序凍結3000毫秒。 – Patashu 2013-05-03 05:42:44

+0

我想做那個Patashu,我怎麼做到的? – 2013-05-03 05:49:25

回答

1

這是應該的

try { 
    Thread.sleep(3000); 
    mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 
} catch (InterruptedException e) { 
    // it depends on your app logic what to do with InterruptedException, you can process it or rethrow or restore interrupted flag 
} 
0

試試這個代碼

protected boolean _active = true; 
// time to display the splash screen in ms 
protected int _splashTime = 1000; 
Thread splashThread = new Thread() { 
@Override 
public void run() { 
try { 
int waited = 0; 
while(_active && (waited < _splashTime)) { 
sleep(100); 
if(_active) { 
waited += 100; 
} 
} 
} catch(InterruptedException e) { 
    e.printStackTrace(); 
    mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 
}