我已经尝试了一个语音控制的应用程序,经过许多示例等,我有以下代码。
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
mySpeechRecognizer.startListening(intent);
speak("I wait for your instuctions");
}
});
和
@Override
public void onResults(Bundle bundle) {
List<String> results = bundle.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION
);
processResult(results.get(0));
}
与
private void processResult(String mycom){
mycom = mycom.toLowerCase();
if(mycom.contains("one")) {
speak("Opening one");
Intent intent = new Intent(MainActivity.this, OneActivity.class);
startActivity(intent);
该应用程序会构建并运行,并显示“我正在等待您的指示”,但在用户指示后不会打开新活动。
有人可以帮助我了解错误之处吗