Google即时与应用集成

时间:2013-11-22 08:04:09

标签: android integration google-now

我正在尝试构建一个语音控制的应用程序,它可以根据命令执行某些任务 我想在其中添加Google即时功能,以便在用户询问有关天气信息,新闻,有关名人等的一些问题时,我可以从Google即时获得结果。

有没有办法在我的应用中集成Google现在的功能?

2 个答案:

答案 0 :(得分:2)

查看Voice Reorganization in Android

您可以按照以下方式实施:

在按钮的点击事件上写下面的代码,该事件负责解除语音意图。

/**
 * Instruct the app to listen for user speech input
 */
private void listenToSpeech() {
    //start the speech recognition intent passing required data
    Intent listenIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //indicate package
    listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    //message to display while listening
    listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word!");
    //set speech model
    listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //specify number of results to retrieve
    listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
    //start listening
    startActivityForResult(listenIntent, VR_REQUEST);
}

当意图回叫时,我们会显示转录的声音。

/**
 * onActivityResults handles:
 *  - retrieving results of speech recognition listening
 *  - retrieving result of TTS data check
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //check speech recognition result
    if (requestCode == VR_REQUEST && resultCode == RESULT_OK)
    {
        //store the returned word list as an ArrayList
        ArrayList<String> suggestedWords = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //set the retrieved list to display in the ListView using an ArrayAdapter
        wordList.setAdapter(new ArrayAdapter<String> (this, R.layout.word, suggestedWords));

     //to open the result in browser 
     Intent intent = new Intent(Intent.ACTION_VIEW, 
     Uri.parse("https://www.google.co.in/?gws_rd=cr#q="+suggestedWords));
startActivity(intent);
    }
    //tss code here
    //call superclass method
    super.onActivityResult(requestCode, resultCode, data);
}

答案 1 :(得分:0)

到目前为止,我找到的最接近你要求的是

  

RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE

这基本上会在您的应用上启动Google即时屏幕,然后像Google现在一样回复语音反馈。

我还没有找到任何方法可以在后台听,并获得可以通过TTS引擎转换为语音的语音结果或文本结果。