2011-07-29 34 views
7

我是android中的新成員,目前正在開發適用於Voice Command API的小型應用程序。例如,如果我說藍牙,它會將手機的藍牙切換到開/關模式(反之亦然)。如何在Android中使用語音命令API

請幫我做這件事....在Anvance

謝謝...

回答

9

這是相當簡單的使用方法:

private void startVoiceRecognitionActivity() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    //uses free form text input 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
     RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    //Puts a customized message to the prompt 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
     getString(R.string.listenprompt)); 
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
} 

/** 
* Handles the results from the recognition activity. 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { 
     // Fill the list view with the strings the recognizer thought it could have heard 
     ArrayList<String> matches = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS); 

     //Turn on or off bluetooth here 
    } else { 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

然後調用從startVoiceRecognitionActivity()內的代碼,無論你需要它。當然,你需要有熱情才能訪問互聯網

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

在你的Android.manifest。

+0

對不起親愛的這麼晚回覆,因爲現在我已經用它。它的工作非常好。 –

+0

我得到了可能的文本值Arraylist ,但我怎麼可以打開/關閉藍牙。 –

+10

這是一個不同的問題,但[lmgtfy](http://goo.gl/aB3be)。 – keyboardsurfer

1

從來沒有用過它,但這個link到Android文檔似乎勾勒的基本思路。 編輯:上一頁鏈接現在分成但是這在Android開發者Link

+2

只是你知道,它現在是一個斷開的鏈接。 – Zammbi

+0

正確的鏈接可能是這個:http://android-developers.blogspot.com/2010/03/speech-input-api-for-android.html – pkout

+0

@pkout謝謝,鏈接已更新 – Elemental