我想在用户按下按钮时启动 Google即时语音搜索。但是,我找不到意图在文档中开始搜索。
有人知道如何开始 Google Now语音搜索的活动吗?
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:9)
private static final int RECOGNIZER_REQ_CODE = 1234;
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent, RECOGNIZER_REQ_CODE);
请注意,您必须使用startActivityForResult()
,因为startActivity()
不受支持。有关详细信息,请参阅上述链接文档。
答案 2 :(得分:8)
您只需将操作设置为android.intent.action.VOICE_ASSIST
即可启动活动,并弹出Google Now语音识别器。使用开发人员工具尝试此操作:
adb shell am start -a android.intent.action.VOICE_ASSIST