我是Android方面的新手,目前正在开发一款基于语音命令API的小型应用程序。例如,当我说“蓝牙”时,它将切换手机的蓝牙开关(反之亦然)。
请帮助我完成这个功能...
提前感谢您的帮助...
我是Android方面的新手,目前正在开发一款基于语音命令API的小型应用程序。例如,当我说“蓝牙”时,它将切换手机的蓝牙开关(反之亦然)。
请帮助我完成这个功能...
提前感谢您的帮助...
使用起来非常简单:
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//uses free form text input
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//Puts a customized message to the prompt
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.listenprompt));
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* Handles the results from the recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
//Turn on or off bluetooth here
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
然后在您的代码中需要时调用startVoiceRecognitionActivity()
。当然,您需要许可才能访问互联网。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
在你的 Android.manifest 文件中。