How to programmatically initiate a Google Now voice search?

懵懂的女人 提交于 2019-12-28 11:45:10

问题


I want to start a Google Now voice search when the user presses a button. However, I can't find the Intent to start the search in the docs.

Does anybody know how to start activity for Google Now voice Search?


回答1:


Use ACTION_RECOGNIZE_SPEECH:

private static final int RECOGNIZER_REQ_CODE = 1234;

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent, RECOGNIZER_REQ_CODE);

Please note that you have to use startActivityForResult() as startActivity() is not supported. See the above linked docs for details.




回答2:


Call Activity for Voice Input:

/* Call Activity for Voice Input */
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

try {
    startActivityForResult(intent, 1);
} catch (ActivityNotFoundException a) {
    Toast.makeText(context, "Oops! Your device doesn't support Speech to Text",Toast.LENGTH_SHORT).show();
}

Get Input from as String:

(I have used for set Text in Search View and Search for that Value)

/* When Mic activity close */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case 1: {
        if (resultCode == Activity.RESULT_OK && null != data) {
            String yourResult = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).get(0);
        }
        break;
    }
    }
}



回答3:


You need to start an Activity with only the Action set to android.intent.action.VOICE_ASSIST and the Google Now Speech recognizer pops up. Try this using the developer tools:

adb shell am start -a android.intent.action.VOICE_ASSIST


来源:https://stackoverflow.com/questions/18049157/how-to-programmatically-initiate-a-google-now-voice-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!