问题描述
我正在做有很多屏幕的仪表板应用程序.当用户根据我需要打开活动时告诉语音命令.我不知道从哪里开始我已经完成了所有屏幕,我想实现语音搜索.我的应用程序屏幕是预付款、休假、招聘、权限、通知等示例:当用户说Advances"时,它应该打开Advances"屏幕.请帮帮我.
I am doing dashboard application in which there are lot of screens. When the user tell the voice command based on that I need to open the activity. I don't know where to start I have already completed all the screens and I would like to implement voice search.my app screens are Advances, Leaves, Recruitment, Permissions, Notifications etcExample: when the user say 'Advances' it should open the advances screens. Please help me.
推荐答案
1) 启动语音识别 Intent
1) Start a voice recognition intent
2) 处理onActivityResult()中返回的数据来决定启动哪个Activity
2) Handle the returned data in onActivityResult() to decide which activity to start
1.启动语音识别意图
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Choose Activity");
startActivityForResult(intent, REQUEST_SPEECH);
2.处理返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_SPEECH){
if (resultCode == RESULT_OK){
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (matches.size() == 0) {
// didn't hear anything
} else {
String mostLikelyThingHeard = matches.get(0);
// toUpperCase() used to make string comparison equal
if(mostLikelyThingHeard.toUpperCase().equals("ADVANCES")){
startActivity(new Intent(this, Advances.class));
} else if() {
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
这篇关于如何根据语音命令打开Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!