2016-01-28 14 views
-3

下面是代碼:如何從語音打開活動以文本輸出

public void promptSpeechInput(){ 
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!"); 

    try{ 
     startActivityForResult(i, 100); 
    } 
    catch(ActivityNotFoundException a){ 
     Toast.makeText(VoiceSearch.this, "Sorry! Your device doesn't support Speech Language!", Toast.LENGTH_LONG).show(); 
    } 

} 

public void onActivityResult(int request_code, int result_code, Intent i){ 
    super.onActivityResult(request_code,result_code,i); 

    switch(request_code){ 
     case 100: if(result_code == RESULT_OK && i != null){ 
      ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      resultTEXT.setText(result.get(1)); 
      String text = String.valueOf(result); 
      Toast.makeText(getApplicationContext(),text, Toast.LENGTH_LONG).show(); 
      if (text.equals("speedometer")){ 
       getWindow().setExitTransition(new Explode()); 
       Intent intent = new Intent(getApplicationContext(), SpeedometerTest.class); 
       startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); 

      } 
     } 
      break; 
    } 
} 

我不知道,如果「如果」語句的工作!

+0

調試你的代碼 –

回答

1
String s = result.get(0); 
if (s.equals("speedometer")){ 
      Intent intent = new Intent(getApplicationContext(), SpeedometerTest.class); 
      startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); 

     } 

希望它能起作用。

+0

非常感謝這個人..它的工作! ;) – Hans

+0

歡迎:-),請接受我的回答 –