19

我想在用户按下按钮时启动Google Now 语音搜索。但是,我在文档中找不到开始搜索的意图。

有人知道如何开始Google Now 语音搜索的活动吗?

4

3 回答 3

11

语音输入的呼叫活动:

/* 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();
}

从字符串获取输入:

(我已用于在搜索视图中设置文本并搜索该值)

/* 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;
    }
    }
}
于 2014-01-08T12:10:52.507 回答
10

使用ACTION_RECOGNIZE_SPEECH

private static final int RECOGNIZER_REQ_CODE = 1234;

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

请注意,您必须使用startActivityForResult()asstartActivity()不受支持。有关详细信息,请参阅上面链接的文档。

于 2013-08-05T06:47:32.383 回答
8

您需要启动一个仅将 Action 设置为的 Activity,android.intent.action.VOICE_ASSIST然后弹出 Google Now Speech 识别器。使用开发者工具试试这个:

adb shell am start -a android.intent.action.VOICE_ASSIST
于 2014-07-04T11:27:17.873 回答