2012-10-13 55 views
2

是否可以通過編程方式打開「Speak Now」對話框?可能以編程方式打開「Speak Now」對話框?

目前,如果用戶點擊我的「搜索」按鈕,會打開一個對話框,我會自動打開軟鍵盤,因此用戶無需點擊文本字段。

我想提供一個替代的「通過語音搜索」來打開對話框並自動打開「現在講話」窗口。所以用戶不必在鍵盤上找到並點擊'mic'按鈕。

任何想法?

回答

3

是的,這是可能的。看看Android SDK中的ApiDemos示例。有一個名爲VoiceRecognition的活動,它使用RecognizerIntent

基本上,你需要做的就是製作一些合適的意圖,然後閱讀結果。

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 

private void startVoiceRecognitionActivity() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    // identifying your application to the Google service 
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName()); 
    // hint in the dialog 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); 
    // hint to the recognizer about what the user is going to say 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    // number of results 
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
    // recognition language 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"en-US"); 
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { 
     ArrayList<String> matches = data.getStringArrayListExtra(
        RecognizerIntent.EXTRA_RESULTS); 
     // do whatever you want with the results 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
相關問題