如果要将语音识别添加到小组的Android应用中,这非常简单。
在整个教程中,您需要在粘贴代码时添加导入。
- 创建一个xml文件或使用现有的xml文件,并确保添加一个按钮和一个listview。
- 在Java类中,您需要扩展活动并实现,
OnClickListener
您将得到一条错误消息,指出您有未实现的方法。将鼠标悬停在它上面并添加未实现的方法。我们将在以后添加。 - 接下来,在Java中设置按钮和Listview。
public ListView mList; public Button speakButton;`` 还添加: public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 4. 接下来,创建一个OnCreate方法并设置按钮和侦听器。
speakButton = (Button) findViewById(R.id.btn_speak);speakButton.setonClickListener(this);
还要添加此方法(我们接下来将对其进行设置) voiceinputbuttons();记住要为要显示的xml设置setContentView。 5. 在oncreate之外,创建一个新的方法,如下所示。
public void voiceinputbuttons() { speakButton = (Button) findViewById(R.id.btn_speak); mList = (ListView) findViewById(R.id.list);}6. 现在,您将必须使用以下代码来设置语音识别活动。
public void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_prompt, "Speech recognition demo"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);}7. 接下来,在步骤2的onclick方法中,添加步骤6的活动。 startVoiceRecognitionActivity(); 8. 接下来,我们将不得不设置另一种方法。复制并粘贴以下代码。
@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));匹配是语音输入的结果。这是用户可能说的话的列表。如果关键字匹配,对要使用的关键字使用if语句可以使用任何活动,可以设置多个关键字来使用同一活动,因此一个以上的单词将允许用户使用该活动(用户不必记住列表中的单词。要使用语音输入信息中的活动,只需使用以下格式;
if (matches.contains("information")) { informationMenu();}注意:您可以随时通过在Eclipse中按ctrl + shift + F来格式化代码。 9. 现在,我们将设置步骤8中的代码使用的方法。此代码创建了一种将用户定向到新菜单的意图。为此,您将需要另一个xml和java类。另外,请记住将活动添加到清单中。
public void informationMenu() { startActivity(new Intent("android.intent.action.INFOSCREEN"));}10. 最后,您需要设置一些代码,以使用户知道麦克风是否可运行。最后,将此代码粘贴到OnCreate方法中。
// Check to see if a recognition activity is present// if running on AVD virtual device it will give this message. The mic// required only works on an actual android devicePackageManager pm = getPackageManager();List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);if (activities.size() != 0) { voiceButton.setonClickListener(this);} else { voiceButton.setEnabled(false); voiceButton.setText("Recognizer not present");}最后说明:语音识别将无法在虚拟模拟器上运行,因为它们无法访问您计算机上的麦克风。语音识别只能在互联网连接下使用。这是大约。您的最终代码在Java中应该是什么样的。
package com.example.com.tutorialthread;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.speech.RecognizerIntent;import android.support.v4.app.NavUtils;public class main extends Activity implements onClickListener {public ListView mList;public Button speakButton;public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); speakButton = (Button) findViewById(R.id.btn_speak); speakButton.setonClickListener(this); voiceinputbuttons();}public void informationMenu() { startActivity(new Intent("android.intent.action.INFOSCREEN"));}public void voiceinputbuttons() { speakButton = (Button) findViewById(R.id.btn_speak); mList = (ListView) findViewById(R.id.list);}public void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_prompt, "Speech recognition demo"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);}public void onClick(View v) { // TODO Auto-generated method stub startVoiceRecognitionActivity();}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, 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 matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches)); // matches is the result of voice input. It is a list of what the // user possibly said. // Using an if statement for the keyword you want to use allows the // use of any activity if keywords match // it is possible to set up multiple keywords to use the same // activity so more than one word will allow the user // to use the activity (makes it so the user doesn't have to // memorize words from a list) // to use an activity from the voice input information simply use // the following format; // if (matches.contains("keyword here") { startActivity(new // Intent("name.of.manifest.ACTIVITY") if (matches.contains("information")) { informationMenu(); } }}```



